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

When it's safe to get rectTransform.position?

Discussion in 'UGUI & TextMesh Pro' started by bdovaz, Jan 16, 2016.

  1. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,011
    I get wrong values if I call it on Awake or on Start if I don't wait a reasonable time (Example: 1 second):

    Code (CSharp):
    1. IEnumerator Start() {
    2.             Debug.Log("Start (0) - Position: " + rectTransform.position);
    3.  
    4.             yield return null;
    5.  
    6.             Debug.Log("Start (1) - Position: " + rectTransform.position);
    7.  
    8.             yield return new WaitForSeconds(1f);
    9.  
    10.             Debug.Log("Start (2) - Position: " + rectTransform.position);
    11. }
    It's there an approach to know where it's safe to get correct values?
     
  2. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Best time to get the values is during LateUpdate, once Unity has finished all it's background processing with the UI
     
  3. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,011
    Yes but I mean If I want to store this value once. That's why I'm using Start.
     
  4. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
  5. SimonDarksideJ

    SimonDarksideJ

    Joined:
    Jul 3, 2012
    Posts:
    1,683
    Yup, doing
    Code (CSharp):
    1. yield return new WaitForEndOfFrame ();
    In a Coroutine in start will do it as you are simply waiting for the first frame to finish and LateUpdate being called before inspecting it.
    but not in Awake!
     
  6. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    1,011
    Thanks but it should be documented that you can't access rectTransform properties (it's not reliable) on Awake or Start without waiting to the first frame.
     
  7. Stephan-B

    Stephan-B

    Unity Technologies

    Joined:
    Feb 23, 2011
    Posts:
    2,269
    I wish Unity would revise their implementation as this is very confusing for many users. Especially when users look at the inspector and see values for Width and Height for instance and then in Awake or Start check those same properties and get values of Zero.
     
  8. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Last edited: Jan 18, 2016
  9. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    It was a bug that it wasn't being reported correctly in Awake and Start but thats since been fixed but will more likely be in 5.4 instead of 5.3
     
  10. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    Thanks for confirming that it's a bug. In the meantime, what are the recommended workarounds, if any?
     
  11. phil-Unity

    phil-Unity

    Unity UI Lead Developer Unity Technologies

    Joined:
    Nov 23, 2012
    Posts:
    1,226
    Really nothing can be done, its a issue on the c++ side.