- Home /
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.
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.
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?
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
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..
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 :)
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 :)