- Home /
Question by
Phedg1 · Feb 15, 2016 at 10:13 PM ·
editor-scriptingupdaterecttransform
RectTransform.rect.width/height Incorrect On Editor Startup
So I have been working on an in editor script when I came across the following problem. My code:
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class anchorTool : MonoBehaviour {
void Update () {
#if UNITY_EDITOR
ownRectTransform = gameObject.GetComponent<RectTransform>();
Debug.Log(ownRectTransform.rect.width);
Debug.Log(ownRectTransform.rect.height);
#endif
}
}
I have found Update is called in the editor once when the scene is loaded. During that first call the values returned for width and height are incorrect. In all subsequent call the values ARE correct. Is there a reason for this and is there any way to get the correct values in that first call?
I already found out that you can't start co-routines in edit mode, so I am feeling lost.
Comment
Answer by Phedg1 · Feb 15, 2016 at 10:31 PM
The solution I found it to delay the script execution by adjusting the script execution order.
Your answer
![](https://koobas.hobune.stream/wayback/20220612064337im_/https://answers.unity.com/themes/thub/images/avi.jpg)