Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Rect Transform not yet set when referencing in Awake

Discussion in 'Scripting' started by greedygoblins, Nov 28, 2014.

  1. greedygoblins

    greedygoblins

    Joined:
    Sep 11, 2013
    Posts:
    4
    I have a script which is trying to reference the RectTransform of a child object in the Awake method. I am referencing the child simply using a public property to which I have assigned the child RectTransform in the editor.

    According to the documentation...
    "Awake is called after all objects are initialized so you can safely speak to other objects or query them..."

    Seems this is not the case when it comes to RectTransforms as the RectTransform of the child is initial during both the Awake and Start methods. It is also initial during the first pass of the Update method!! Surely this is not right? Am I doing something wrong?

    [Edit] I should clarify... it appears to be properties such as 'rect' and 'anchoredPosition' which aren't set at that point. It appears that 'position' is set. I'd be interested to know why the aforementioned properties are not set.
     
    Last edited: Nov 28, 2014
    georgepiva likes this.
  2. greedygoblins

    greedygoblins

    Joined:
    Sep 11, 2013
    Posts:
    4
    Just to clarify what's going on... see the image below...



    The RectTransforms don't get set to the correct values until the 2nd pass of Update(). Can't fathom out what the heck is going on here. Can anyone help?
     
    georgepiva likes this.
  3. GraphiteGames

    GraphiteGames

    Joined:
    Apr 29, 2015
    Posts:
    5
    @greedygoblins I am seeing some similar discrepancies in Awake() vs. Start() vs. Unity Editor.

    For example, if I look at an object transform.rotation in my editor it will have a x angle of 30 degrees. I hit play. The angle in the editor still says 30 degrees. Reading out the value of x during Awake() is 0 degrees. Reading the SAME value x during Start() is 0.25858 (varies slightly from run to run). WTF is going on between start and update? And why is x not getting updated to 30 degrees from the editor??? Were you able to resolve this? Anyone else help?

    I've looked around and am not seeing other answers yet. I'm using Unity 5.0.1.f1. Thanks in advance.
     
    georgepiva likes this.
  4. Suminsky

    Suminsky

    Joined:
    Aug 11, 2011
    Posts:
    50
    Im having the same problem, but not in the first run, just after using SceneManager.LoadScene, the rects on awake are not set till it reaches update (not the second pass like you, but in the first its already set, but not in awake).

    Also, this is very undefined, when it happens, it always happens, when it doesnt happen, it never happens. Depends on luck each time you open unity.
    Im using version 5.3.3f1.
    Anyone know if this was ever addressed?

    Another very anoying problem is when you use the anchors so that positions are always correct despite resolution changes, inspector keep setting F***edup positions for every recttransform at reset, instead of keeping it at 0 (which is the value that makes the anchors work). I build a editor script to set every rect child (recursively) of current selected in inspector/hierarchy to zero to stop this mess.
     
  5. gresolio

    gresolio

    Joined:
    Jan 5, 2014
    Posts:
    17
    Probably some layout component (such as HorizontalLayoutGroup, VerticalLayoutGroup, etc) is used. If that the case, then RectTransform values are not initialized correctly until first update. There are some tricky solutions to force update manually: Force Immediate Layout Update. Alternatively you can remove/disable layout component if possible, or save the necessary values in another way.