- Home /
OnDrawGizmos() called in editor without Start()?
I'm just learning the ins and outs of scripting in Unity, so I'm probably missing something very basic here...
OnDrawGizmos() and OnDrawGizmosSelected() appear to be called by the Unity editor without the Start() method being called first. I get that the Start() method is only called at runtime, but it's supposed to be where I initialize my script's state, isn't it? If the OnDraw_() methods are called without any regards to Start(), how am I supposed to initialize my script?
I'm guessing something like this...
public class MyScript : MonoBehaviour {
private bool myInitFlag = false;
// Other data members here
.
.
.
void Start() {
Init();
}
void OnDrawGizmos() {
Init();
}
void OnDrawGizmosSelected() {
Init();
}
void Init() {
if (!myInitFlag) {
// Initialize other data members . . .
myInitFlag = true;
}
}
}
...but this seems awfully awkward to me, since it necessitates checking whether the script's been initialized every single time one of the OnDraw_() methods is called. Is this what Unity is expecting me to do, or is there a better way of doing this that I'm missing?
Thanks!
Answer by Bunny83 · Jul 23, 2016 at 01:25 PM
You might have not fully understood what OnDrawGizmos and OnDrawGizmosSelected are good for. They just allows you to display a custom gizmo for your object inside the editor. They are ment to show the position of the object and allows the user to select the object by clicking on the gizmo.
If your script should work in the editor as well you might want to use the ExecuteInEditMode attribute on your class. This will make "Start" and "Update" being called inside the editor. However using ExecuteInEditMode can cause a lot of problems and if not handled with care can cause data corruption. Keep in mind that any changes to objects at edittime are permanent. So when you use ExecuteInEditMode make sure your Start / Update methods don't mess around with the wrong stuff. Also Update isn't called on a regular basis at edittime, only when something get changed / updated.
It's difficult to seperate pure editor code from pure runtime code that way. That's why it's usually better to write a seperate editor script if you need advanced editor features.
You have to be very careful what you actually initialize at edit time.
Oh, that is a beautiful, beautiful thing. The ExecuteInEdit$$anonymous$$ode class attribute is exactly the missing link I was looking for. Even the selective Update() calls are perfect. Thank you so much for your answer, Bunny83, complete with caveats!
Answer by miahmiah · Jul 23, 2016 at 01:47 PM
A gizmo is in the editor, and therefore must know what to do before start is pressed. The gizmo should be a seperate script and have it reference other objects with already set properties.
Answer by xCyborg · Oct 16, 2019 at 03:23 PM
I have the same problem, I want it to execute only in RUNTIME mode NOT edit mode, how can I do?
Answer by DSivtsov · Nov 11, 2021 at 06:58 PM
By this way (in my case the p1, p2 and _character are calculated only at Runtime mode (Runtime mode in Editor !!!)
private void OnDrawGizmos()
{
if (Application.IsPlaying(this))
{
CheckGroundTouchAtPoint();
Gizmos.color = Color.red;
Gizmos.DrawWireSphere(p1, _character.radius);
Gizmos.DrawWireSphere(p2, _character.radius);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612085322im_/https://answers.unity.com/themes/thub/images/avi.jpg)