Author Topic: Why can't my code change my UILabel position after instantiation?  (Read 3331 times)

alexh0101

  • Newbie
  • *
  • Thank You
  • -Given: 4
  • -Receive: 0
  • Posts: 30
    • View Profile
Why can't my code change my UILabel position after instantiation?
« on: September 26, 2014, 08:57:17 PM »
I'm not able to change the position of neither the UILabel prefab, nor its empty parent gameObject.
theParent stays at (0,0,0);

Here is my code:

  1. GameObject theParent = new GameObject();
  2. theParent.layer = 11; // the GUI layer
  3. theParent.transform.name = "MessageParent";
  4. theParent.transform.position = new Vector3(0,5,0); // Is there anything wrong here?
  5.  
  6. GameObject comboInstance = NGUITools.AddChild(theParent,messagePrefab);

I'm trying to move the parent to avoid interfering with the animation of the child.

Note: messagePrefab has the following components: UILabel, an Animator and a "auto-destroy" script.
An animation changes its size and position; it starts right when the prefab is spawned.

Why can't my code change my UILabel position after instantiation?

Thanks in advance
« Last Edit: September 27, 2014, 07:40:11 AM by alexh0101 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Why can't my code change my UILabel position after instantiation?
« Reply #1 on: September 27, 2014, 07:14:38 PM »
Well, let's examine what you're doing...

1. You create a new game object in the middle of nowhere, change its layer, name and position.
2. You then use AddChild to add an instance of "messagePrefab" to the newly added game object.

You can't add UI elements in the middle of nowhere. They must be children of panels at all times. You never made a panel. You just created a random game object.