Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: delzhand on February 06, 2014, 09:35:25 PM

Title: NGUITools.AddChild creates duplicate objects
Post by: delzhand 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?
Title: Re: NGUITools.AddChild creates duplicate objects
Post by: delzhand 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!
Title: Re: NGUITools.AddChild creates duplicate objects
Post by: peterworth 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.
Title: Re: NGUITools.AddChild creates duplicate objects
Post by: Mourkain 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?
Title: Re: NGUITools.AddChild creates duplicate objects
Post by: Nicki 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.
Title: Re: NGUITools.AddChild creates duplicate objects
Post by: Mourkain on June 01, 2015, 06:12:56 AM
Thanks, just wrote an own function now and it works ;)