Categories
Development Game Creation

Hello Every one, I was studding A* Algorithm and its is Showing nullObjectException n OnGizmos Function

I was studding A* Algorithm and its is Showing nullObjectException n OnGizmos Function when ever it complete 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));
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *