Author Topic: NGUI Object position is being moved automatically after instantiation  (Read 1247 times)

knguyen

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Hi,

I have the following code:

  1. IEnumerator Populate(string part) {
  2.   GameObject newItem = (GameObject)Instantiate (ItemPrefab);
  3.                                 UISprite image = newItem.GetComponent<UISprite>();
  4.   image.spriteName = "Head";
  5.                                 image.MakePixelPerfect();
  6.                                 image.width = image.width/4;
  7.                                 image.height = image.height/4;
  8.                                 newItem.SetActive(true);
  9.                         Debug.Log (newItem.transform.position);
  10.                                 newItem.transform.position = new Vector3(1000f,1000f,1000f);
  11.                         Debug.Log (newItem.transform.position);
  12.                         yield return new WaitForSeconds(1);
  13.                         Debug.Log (newItem.transform.position);
  14. }
  15.  

The three debug logs return the following (in order):
(-128.0, 49.0, 0.0)
(1000.0, 1000.0, 1000.0)
(22.6, 3.1, -7.0)

So AFTER the yield (the wait for 1 second), the position of the object is getting updated automatically. If I set the position of the object AFTER the 1 second, the position sticks. Please explain this to me.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Never use Instantiate.

Use NGUITools.AddChild(parent, prefab).

Look inside the function to find out why.