Can't Create New MonoBehavior Using New - Help?

Hey guys, I’m trying to make an A* pathfinding grid, issue is, the way I’m doing it isn’t allowed by Unity! Apparently trying to create a new MonoBehavior using “new Node” you get the following warning: “You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all”

As a result, all of my nodes come back “null” in my grid. Major issue. I don’t think I can use AddComponent because I have to have these Nodes in an array form. Would ScriptableObject fulfill what I need? How would I go about doing that? Just a reminder, I have to have my Nodes in my nodeGrid array. Here’s my code:

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

public class Pathfinding : MonoBehaviour {

    public float nodeSize;
    Node[,,] nodeGrid;

    public int gridSizeX;
    public int gridSizeY;
    public int gridSizeZ;
    public Vector3 cornerA;
    public Vector3 cornerB;

    int indexX = 0;
    int indexZ = 0;
    int indexY = 0;
    Vector3 nodePos;
    bool walkable;

    int distanceX;
    int distanceY;
    int distanceZ;

    public GameObject player;

    void Start () {

        //Finds the grid size
        gridSizeX = Mathf.RoundToInt (cornerA.x - cornerB.x); //If the two are subtraced you find the distance
        gridSizeY = Mathf.RoundToInt (cornerA.y - cornerB.y);
        gridSizeZ = Mathf.RoundToInt (cornerA.z - cornerB.z);
        gridSizeX = Mathf.RoundToInt (gridSizeX / nodeSize);
        gridSizeY = Mathf.RoundToInt (gridSizeY / nodeSize);
        gridSizeZ = Mathf.RoundToInt (gridSizeZ / nodeSize);

        gridSizeX *= -1; //debug
        gridSizeY *= -1;

        if (gridSizeY < 0) {

            gridSizeY = 1;

        }


        //Creates the grid
        nodeGrid = new Node[gridSizeX, gridSizeY, gridSizeZ];
        nodePos = cornerA;

        while (indexY < gridSizeY) {

            while (indexZ < gridSizeZ) {

                while (indexX < gridSizeX) {
            
                    nodePos.x += nodeSize;
                    walkable = !Physics.CheckSphere (nodePos, nodeSize);
                    nodeGrid [indexX, indexY, indexZ] = new Node (walkable, nodePos);
                    print (nodeGrid [indexX, indexY, indexZ]);
                    indexX++;

                }

                //Shifts it next to the Z axis, resets the X
                indexX = 0;
                nodePos.z -= nodeSize;
                nodePos.x = cornerA.x;
                indexZ++;

            }

            //Shifts it up the Y axis, resets the X and Z
            nodePos.x = cornerA.x;
            nodePos.z = cornerA.z;
            nodePos.y += nodeSize;
            indexZ = 0;
            indexY++;

        }

    }

    public Node LocatePosition(Vector3 position){

        distanceX = Mathf.RoundToInt (position.x - cornerB.x);
        distanceY = Mathf.RoundToInt (position.y);
        distanceZ = Mathf.RoundToInt (position.z - cornerB.z);

        distanceX = Mathf.RoundToInt (distanceX / nodeSize);
        distanceY = Mathf.RoundToInt (distanceY / nodeSize);
        distanceZ = Mathf.RoundToInt (distanceZ / nodeSize);

        distanceX *= -1;
        return nodeGrid [distanceX, distanceY, distanceZ];

    }

    void OnDrawGizmos(){

        Node playerNode = LocatePosition (player.transform.position);
        Debug.Log (playerNode);
        foreach (Node n in nodeGrid) {

            if (n.walkable == false) {

                Gizmos.color = Color.red;
                Gizmos.DrawCube (n.position, Vector3.one * nodeSize / 2);

            }

            if (n == playerNode) {

                Gizmos.color = Color.blue;
                Gizmos.DrawCube (n.position, Vector3.one * nodeSize / 2);

            }
        }

    }

}

It would be useful to see the node class aswell. Why does your node class have to use a MonoBehaviour, what functionality does it use that relies on inheriting from it?

Woops, good point! Thanks! I forgot that MonoBehavior was something in the script. Lol! I doesn’t need that.

2 Likes