Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
3
Question by Syndog · Jul 22, 2016 at 03:37 PM · c#scriptingbasicsinitializationondrawgizmos

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Syndog · Jul 23, 2016 at 02:02 PM 0
Share

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!

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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?

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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);
         }
     }


Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Initialising List array for use in a custom Editor 1 Answer

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

How can I compare a sequence of GameObjects when tapped? 1 Answer

Must I attach every script to a gameobject in order to work ? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges