Author Topic: NGUITools.AddChild creates duplicate objects  (Read 5245 times)

delzhand

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 6
    • View Profile
NGUITools.AddChild creates duplicate objects
« on: February 06, 2014, 09:35:25 PM »
I've been using the demo version of NGUI for about a year, and I finally bought the license for the full version today.  While most of it makes a lot of sense, I'm not sure I understand the "correct" way to do things when creating things programmatically.

I think I'm calling NGUITools.AddChild incorrectly in some circumstances.  If I call it like this:

  1. NGUITools.AddChild(uiRoot, new GameObject("Test"));

it works like I expect, it creates a basic GameObject (although it does add "(Clone)" to the name, which is irksome but fixable).

However, when I try to instantiate an actual prefab, like so:

  1. NGUITools.AddChild(uiRoot, Instantiate(Resources.Load("Prefabs/MyPrefab")));

then MyPrefab(Clone) is instantiated in the hierarchy root, and 'New Game Object' is created as a child of uiRoot.

Thinking that I'd done it wrong and that maybe the prefab needs to be pre-instantiated, I tried:

  1. NGUITools.AddChild(uiRoot, (GameObject)UnityEngine.Object.Instantiate(Resources.Load("Prefabs/MyPrefab")));
  2.  

but that creates MyPrefab(Clone) in the hierarchy root, and MyPrefab(Clone)(Clone) as a child of uiRoot.

At this point I took a look at the actual code of AddChild, and saw that it actually calls GameObject.Instantiate on line 386.  So I tried this:

  1. NGUITools.AddChild(uiRoot, Resources.Load("Prefabs/MyPrefab"));

which creates 'New Game Object' as a child of uiRoot, but it's just a basic GameObject with no components, meshes, etc.

At this point I'm at a loss, the code of AddChild isn't especially complex, so it has to be with my call, right?

delzhand

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 6
    • View Profile
Re: NGUITools.AddChild creates duplicate objects
« Reply #1 on: February 06, 2014, 09:39:52 PM »
Mystery solved!

The last call was closest to correct.  However, Resources.Load() returns an object, not a GameObject, so C# was interpreting it as a bool and calling the AddChild(GameObject, bool) instead of AddChild(GameObject, GameObject).

I hope this helps someone else in the future!

peterworth

  • Newbie
  • *
  • Thank You
  • -Given: 1
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: NGUITools.AddChild creates duplicate objects
« Reply #2 on: May 02, 2014, 09:18:00 AM »
YES!!!

thanks so much for posting the answer, i've spent about 2 days stuck before finding this.

So if using Resources.Load in combination with NGUITools.AddChild that's all you have to do - just make sure the result of the load is cast to GameObject. Phew.

Mourkain

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: NGUITools.AddChild creates duplicate objects
« Reply #3 on: May 27, 2015, 02:48:56 AM »
I have a similar problem.

I have this call, that creates one GameObject under the Parent and one in the Hierarchy root.
  1. NGUITools.AddChild(creatureHolder, SingletonCreatureCreator.Instance().makeCreature(creatures[_arrayNumber]));
  2.  

 Looking into the NGUI-Code the AddChild(GameObject, GameObject) uses Instantiate and thats the Reason why it's doing this.

It would fix it, if I just change this:
  1. static public GameObject AddChild (GameObject parent, GameObject prefab)
  2.         {
  3.                 // GameObject go = GameObject.Instantiate(prefab) as GameObject;
  4.                 Debug.Log("I changed NGUI-Code here.");
  5.                 GameObject go = prefab;

Is there a better solution for this problem?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: NGUITools.AddChild creates duplicate objects
« Reply #4 on: May 31, 2015, 11:34:03 AM »
You're using AddChild all wrong.

AddChild is a replacement for using the raw Instantiate - it creates a new object, sets layers and placement in the hierarchy. I expect your makeCreature doesn't load a prefab, but rather Instantiates a new gameobject, but then AddChild also instantiates that gameobject creating a copy. The prefab gameobject is special in that it doesn't really exist in the hierarchy, but the copy you make probably does; that's why you get a duplicate.

Instead just set the parent of the transform and the layer to the right thing and don't use the AddChild.

Mourkain

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: NGUITools.AddChild creates duplicate objects
« Reply #5 on: June 01, 2015, 06:12:56 AM »
Thanks, just wrote an own function now and it works ;)