Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
0
Question by rivermalachi · Apr 30, 2018 at 08:33 PM · errornewbiegizmoserrormessageondrawgizmos

Tried to call a Gizmos drawing method in OnDrawGizmos but got an error saying I can't call drawing methods outside of OnDrawGizmos...

Hi!
Let me begin by saying that, well. I'm the newbiest of newbs to Unity (I'm pretty sure), and the language barrier isn't helping. I've looked up my question in google and in this forum, but found no answer so I thought I'd ask for help.

I've been working on a procedural navmesh (for a 2D tile based game) and before I can start with the actual A* algorithm I need to define unwalkable and walkable areas.

So, for that, I've made a grid class (I followed Sebastian Lague's tutorial on YouTube) and inside the grid class (which inherits Monobehaviour) I have various functions as well as the OnDrawGizmos() function:

Here's the code:

      void OnDrawGizmos ()
     {
         Gizmos.DrawWireCube(transPos, new Vector3 ((float)gridSizeX, 1, (float)gridSizeY));
         if (grid != null) {
             foreach (Node n in grid) {
                 //if its walkable itll be white, otherwise red.
                 Gizmos.color = (n.walkable) ? Color.white : Color.red;
                 Gizmos.DrawCube (n.worldPosition, Vector3.one * (nodeDiameter - 0.1f));
             }
         }
     }



All variables have been declared beforehand (it's not what my error is about, but thought I'd say so if it was actually somehow related) and I've made sure that the game runs fine without the OnDrawGizmos() call.

When I do call it, the mesh I texture in another class doesn't get textured (the mesh appears, its texture is empty) and an object I instantiate through code doesn't get instantiated.

Here is my error:


UnityException: Gizmo drawing functions can only be used in OnDrawGizmos and OnDrawGizmosSelected. UnityEngine.Gizmos.DrawWireCube (Vector3 center, Vector3 size) (at C:/buildslave/unity/build/artifacts/generated/common/runtime/GizmoBindings.gen.cs:52)



It(the error message) mentions more lines after that, in other files/classes, but I don't understand what it means ):
I call OnDrawGizmos from the grid class' constructor, could that be causing the problem? I haven't seen anything about that though. Other than that.. I'm stuck.I can't think of anything causing this, I don't know why it says that I call the the draw method outside of the OnDrawGizmos()...

Please be kind! This is my first time posting a question in Unity Answers, and I did my best in searching this question with different wordings to make sure it's not a duplicate. I'm really sorry if it is.

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

2 Replies

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

Answer by Bunny83 · Apr 30, 2018 at 08:42 PM

OnDrawGizmos is a callback from the Unity editor. You never should call this method yourself. The same holds true for any callback. That includes Update, Start, Awake, FixedUpdate, OnMouseDown, ..... Callbacks could be raised at different stages of a frame. While Start, Update, FixedUpdate and most of the game logic runs during the "update" period, any direct rendering methods can only be called during the rendering period. Any rendering before the beginning of the rendering phase it pointless since the first thing that happens when Unity starts rendering is clearing the screen.


Keep in mind that OnDrawGizmos as well as the "Gizmos" class is only called / used inside the Editor.

Comment
Add comment · Show 3 · 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 rivermalachi · Apr 30, 2018 at 08:52 PM 0
Share

Thank you for answering! I think I understand now (mostly). But then, how do I get it to draw? Or should I find a different method to draw the grid altogether..? Also I'm having difficulties understanding your last sentence. Does that mean I shouldn't write OnDrawGizmos functions in classes at all?

avatar image Bunny83 rivermalachi · Apr 30, 2018 at 11:20 PM 1
Share

About the last sentence: No, OnDrawGizmos is a callback specifically for runtime classes but it has no effect at runtime / in a build. So it can only be used for debugging inside the editor or for additional editor guidance.


$$anonymous$$eep in $$anonymous$$d that gizmos can be enabled / disabled independently in the game view and in the scene view.


If you still have any problems you should add more relevant information to your question. Do you have that class actually attached to a gameobject? Since you mentioned the constructor you probably do something wrong. You should ignore the constructor of components

avatar image rivermalachi Bunny83 · May 01, 2018 at 06:49 AM 0
Share

Ah I should ignore the constructor altogether?? Um... The Grid class is attached to an A* object. The grid is made of a 2D array of nodes, so I initialize it as completely unwalkable.

Then, through another script (where I texture my map mesh by tiles) I call a function for changing whether a certain node is walkable or not through the grid (The function is inside the Node class, so I call it through the grid array).

I'm not sure if this is the best approach, but it's what I thought of- making the entire grid unwalkable at initialization (because there are more unwalkable parts than walkable), then when I texture the map I check if a texture is one of the unwalkable ones and according to that change the corresponding node to walkable through my WalkableToggle function.

Also, thank you for explaining OnDrawGizmos to me! You've been super helpful :)

EDIT: I thought I'd mention that other than the Grid component, A* is an empty object!

sorry, another edit! - if I want the A* object's dimensions (they are the grid's dimensions) to be different from level 1 to 2 (I have two levels for this game which are similar enough that if I figure one of them I've practically got the other one), should I just stick an A* object in each scene and change the parameters through the inspectors? Or would it be better to do something else..

avatar image
1

Answer by bhavinbhai2707 · Apr 30, 2018 at 08:48 PM

You don't have to call OnDrawGizmos() From anywhere, it is a editor function to display gizmos in the scene-view and is called automatically called!!

Tip from my past experience :- if you are newbie then you should never jump to complex things and huge games, always start from the bottom and make small and easy games and climb slowly !! You say you are newbie and you are trying to implement complex algorithms like A* in path finding type games is too much for a beginner!! Sebastian lague is a Great Game Developer and his videos are mainly focused for advanced developers and contains some very very complex stuff!!

keep learning cheers :)

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 rivermalachi · May 01, 2018 at 06:43 AM 0
Share

I agree on that T_T... But I mean it isn't my first game. Still I'm much too new for this, but I have to at least try and finish it because I'm not doing it just on my spare time but as an external school project. :) I'm having a lot of fun learning!

EDIT: Oh, and thank you for explaining OnDrawGizmos so simply and clearly!! It's really hard for me to understand proper lingo since I'm not a native English speaker and everything I learned about program$$anonymous$$g is in my own language :)

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

109 People are following this question.

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

Related Questions

Im trying to make my character jump while me also having the option to double jump, but im getting an error. can someone review the code please? 0 Answers

Object reference not set to an instance of an object... again 1 Answer

How to solve Error “ImportException: Cannot import name RAND_egd” on IronPython in Unity 0 Answers

Assets\PlayerController.cs(29,29): error CS0200: Property or indexer 'Vector3.normalized' cannot be assigned to -- it is read only 1 Answer

Why can't I see my Gizmos Line?? 0 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