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.

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.

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 :slight_smile: