Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Unity 4.6.3 RectTransform SetParent In Awake

Discussion in 'UGUI & TextMesh Pro' started by ForceX, Feb 20, 2015.

  1. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Bug Case 673933

    Edit: Feb-20 (Changed title to reflect cause of issue & updated the description for the cause of issue)

    Unity 4.6.3 has an issue with changing a RectTransform parent during Awake(). If SetParent is called from Awake() the RectTransform.localScale will be set to Vector3.zero. If the same method of changing a RectTransform's parent is called during Start() or Update() then the RectTransform.localScale will correctly be Vector3.one.

    Again this is new to Unity 4.6.3 & possably 4.6.2p2 as mentioned from this other post i just discovered.
    http://issuetracker.unity3d.com/iss...nsforms-parent-in-awake-set-its-scale-to-00-0

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using UnityEngine.UI;
    4. using System.Collections;
    5.  
    6. public class Make_UI_Image : MonoBehaviour {
    7.  
    8.     void Awake () {
    9.  
    10.         //Procedurally generated UI elements created during Awake have the RectTransform.localScale incorrectly set to Vector3.zero
    11.  
    12.         GameObject newImage = new GameObject("Image_Awake");
    13.         newImage.layer = 5;
    14.         newImage.AddComponent<Image>();
    15.         newImage.GetComponent<RectTransform>().SetParent(GameObject.Find("Canvas").transform);
    16.     }
    17.  
    18.     void Start () {
    19.  
    20.         //Procedurally generated UI elements created during Start have the RectTransform.localScale correctly set to Vector3.one
    21.  
    22.         GameObject newImage = new GameObject("Image_Start");
    23.         newImage.layer = 5;
    24.         newImage.AddComponent<Image>();
    25.         newImage.GetComponent<RectTransform>().SetParent(GameObject.Find("Canvas").transform);
    26.     }
    27. }
    28.  
     

    Attached Files:

    Last edited: Feb 20, 2015
    SunnySunshine likes this.
  2. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    As I understand it, the rule of thumb is to not assume other components are initialised in your Awake methods. I had various issues when moving from one Unity version to another once where I was doing things in Awake that should really have been in Start.
     
  3. virror

    virror

    Joined:
    Feb 3, 2012
    Posts:
    2,963
    Yes, as far as i understand it, references and stuff is good to have in Awake, and actual functionality should go in Start.
     
  4. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    So I did a test to remove some initialization possibilities and it actually has nothing to do with creating a new UI element. The scaling issue comes entirely from changing the parent of a UI element during the Awake(). If you have a pre-existing UI element and for what ever reason you change it's parent then you will loose its scale.
     
  5. andymads

    andymads

    Joined:
    Jun 16, 2011
    Posts:
    1,614
    Probably the safest thing is to not try to affect other GameObjects/Components from Awake. I remember one issue where I was doing a GetComponentsInParent in my Awake and it stopped working after doing a Unity update. I reported a bug and Unity told me "using Awake() both for accessing parent and child transforms is relatively unsafe."
     
  6. ForceX

    ForceX

    Joined:
    Jun 22, 2010
    Posts:
    1,102
    Last edited: Feb 20, 2015
    SunnySunshine likes this.
  7. SunnySunshine

    SunnySunshine

    Joined:
    May 18, 2009
    Posts:
    952
    Thanks for pointing this out. Using start from now on for UI thingies.