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.