Author Topic: [Solved] Trouble Instantiating UI Objects.  (Read 1731 times)

misterkeeter

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
[Solved] Trouble Instantiating UI Objects.
« 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.
« Last Edit: May 23, 2014, 04:31:13 PM by misterkeeter »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trouble Instantiating UI Objects.
« Reply #1 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).

misterkeeter

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: Trouble Instantiating UI Objects.
« Reply #2 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;
   }