0
\$\begingroup\$

I was studding A* Algorithm and it is showing a nullObjectException in my OnDrawGizmos function whenever it finishes compiling the code.

Here is my code - can anybody tell me what I am doing wrong?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace PathFinding {
    public class Grid : MonoBehaviour {
        public Transform Startpostion;
        public LayerMask Layer_Mask;
        public Vector2 gridWorldSize;
        public float nodeRadius;
        public float Distance;

        public Node[ , ] grid;
        float NodeDiameter;
        int GridSizeX, GridSizeY;
        public List<Node> FinalPath;

        private void Start () {
            NodeDiameter = nodeRadius / 2;
            GridSizeX = Mathf.RoundToInt (gridWorldSize.x / 2);
            GridSizeX = Mathf.RoundToInt (gridWorldSize.y / 2);
            GridCreator ();

        }
        void GridCreator () {
            grid = new Node[GridSizeX, GridSizeY];
            Vector3 bottomLeft = transform.position - Vector3.right * gridWorldSize.x / 2 - Vector3.forward * gridWorldSize.y / 2;
            for (int y = 0; y < GridSizeY; y++) {
                for (int i = 0; i < GridSizeX; i++) {
                    Vector3 WorldPositionOfCurrentNode = bottomLeft + Vector3.right * (i * NodeDiameter + nodeRadius) + Vector3.forward * (y * NodeDiameter + nodeRadius);
                    bool Wall = true;

                    if (Physics.CheckSphere (WorldPositionOfCurrentNode, nodeRadius, Layer_Mask)) {
                        Wall = false;
                    }
                    grid[i, y] = new Node (Wall, WorldPositionOfCurrentNode, i, y);
                }
            }
        }
        private void OnDrawGizmos () {
            Gizmos.DrawWireCube (transform.position, new Vector3 (gridWorldSize.x, 1, gridWorldSize.y));

            foreach (Node n in grid) {
                if (n.is_Wall) {
                    Gizmos.color = Color.white;
                } else {
                    Gizmos.color = Color.yellow;
                }
                if (FinalPath != null) {
                    if (FinalPath.Contains (n)) {
                        Gizmos.color = Color.red;
                    }
                }
                Gizmos.DrawCube (n.postion, Vector3.one * (NodeDiameter - Distance));
            }
        }
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Please don't forget to search for existing Q&A to guide your troubleshooting. \$\endgroup\$ – DMGregory Mar 8 '20 at 2:33
  • \$\begingroup\$ ok brother and by the way thanks \$\endgroup\$ – Ali Raza Mar 9 '20 at 6:55
1
\$\begingroup\$

Try adding this to the top of your OnDrawGizmos function:

 if(grid == null || grid[0,0] == null)
     return;

Remember that OnDrawGizmos runs in the editor, even when the game isn't running. So your Start method that calls GridCreator and populates your grid hasn't run yet.

Without this code running to provide initial values, your grid variable is null and contains no valid nodes to loop over.

So, you can either force initialization of your grid before trying to work with it in OnDrawGizmos, or abort gizmo-drawing until the data is populated.

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.