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:
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:
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.
// GameObject go = GameObject.Instantiate(prefab) as GameObject;
Debug.Log("I changed NGUI-Code here.");
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 ;)