[C#] Developing A Node Graph, Need Help With An Issue

Currently I’m developing a node graph for A* and its pretty much working except for one aspect and that is the fact that I’m getting an error message when I run my bit of code.

I get the following: “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”

I’m currently just trying to build a two dimensional array of nodes and for each node I’m just creating a new instance of a class. Here’s my code. The error is given in line 35 where I call my CreateGrid() function.

using UnityEngine;
using System.Collections;

public class GraphGeneration : MonoBehaviour
{

    NodeScript[,] NodeGraph;

    bool maxY;
    bool maxX;

    int aphX; //"aph" means "Array Placeholder"
    int aphY;
    int gridSizeX;
    int gridSizeY;

    Vector3 position;
    bool walkable;
    RaycastHit hit;

    public GameObject placeholder;

    //Temp. piece of code saved

    void Awake()
    {

        //The uniform size of the rooms
        gridSizeX = 80;
        gridSizeY = 60;

        //Temporary
        position = new Vector3(-0.79f, 0.59f, 0);

        CreateGrid();

    }

    void CreateGrid()
    {

        //Fills the array for the graph
        NodeGraph = new NodeScript[gridSizeX, gridSizeY];

        //Fills out the graph by first going through the X component of each Y component until it maxes out.
        while (aphY < gridSizeY)
        {

            while (aphX < gridSizeX)
            {

                position.x += .01f;
                if (Physics.CheckSphere(position, .01f) == true) { walkable = false; } else { walkable = true; }
                NodeGraph[aphX, aphY] = new NodeScript(walkable, position);
                aphX++;

                if(walkable == false) { GameObject.Instantiate(placeholder); }

            }

            aphY++;
            position.y -= 0.01f;
            aphX = 0;

        }

    }

}

Any help would be genuinely appreciated. I cannot think of another way I can fill my array and with what. Will I have to rework my code? (again)

You haven’t shown where the real problem is. That’d be in NodeGraph, where apparently you have declared that it derives from MonoBehaviour.

It can’t do that. Not if you want to create these things with new, and treat them as plain old C# objects. Delete the MonoBehaviour base, and you should be all set.

1 Like

Thanks! Can you explain why MonoBehaviors aren’t allowed to be created using “new”? I don’t understand.

MonoBehaviours are supposed to be used only as components on a GameObject.

Don’t inherit from MonoBehaviour if you’re creating a generic class that isn’t a component.

Note, if not a MonoBehaviour messages like ‘Awake’ no longer are a thing… and instead you use the constructor method to initialize (a method with the same name as the class).

Because a MonoBehaviour is a Component, which is intended to be attached to a GameObject. This is fundamental to how the whole Unity runtime works.

When you create one with new, then it’s not attached to any GameObject. It’s an orphan. And whole piles of assumptions, running deeply throughout the runtime, are invalid. Cats and dogs living together, mass chaos!

So, a few years back (IIRC) they made it so that you simply can’t create such abominations. It’s an error to do so. It was almost certainly an error before (so the reasoning went), so it’s better to flag it as such right away.

1 Like