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)