Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: misterkeeter on May 22, 2014, 09:40:47 PM

Title: [Solved] Trouble Instantiating UI Objects.
Post by: misterkeeter on May 22, 2014, 09:40:47 PM
This following code always creates the prefab at 0,0,0.
I'm not really sure why.

void Start ()
{
        GameObject New = Instantiate(instance, transform.localPosition , transform.localRotation)as GameObject;
   Vector3 pos = new Vector3 (-50f, -250f, 0f);
   New.gameObject.transform.position = pos;
}

Any Help is appreciated.
Title: Re: Trouble Instantiating UI Objects.
Post by: ArenMook on May 23, 2014, 03:49:01 PM
You should not be using Instantiate() with UI elements. UI elements need to be parented properly from the very start. Use NGUITools.AddChild(parent, prefab).
Title: Re: Trouble Instantiating UI Objects.
Post by: misterkeeter on May 23, 2014, 04:30:50 PM
Thanks that worked.

Here's my modified code that performed as expected.

   void Start () {
      GameObject New = NGUITools.AddChild (myUIRoot, instance);
      Vector3 pos = new Vector3 (-50f, -250f, 0f);
      New.transform.localPosition = pos;
   }