Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

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

Discussion in 'Scripting' started by SwaggyMcChicken, Dec 8, 2016.

  1. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    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.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GraphGeneration : MonoBehaviour
    5. {
    6.  
    7.     NodeScript[,] NodeGraph;
    8.  
    9.     bool maxY;
    10.     bool maxX;
    11.  
    12.     int aphX; //"aph" means "Array Placeholder"
    13.     int aphY;
    14.     int gridSizeX;
    15.     int gridSizeY;
    16.  
    17.     Vector3 position;
    18.     bool walkable;
    19.     RaycastHit hit;
    20.  
    21.     public GameObject placeholder;
    22.  
    23.     //Temp. piece of code saved
    24.  
    25.     void Awake()
    26.     {
    27.  
    28.         //The uniform size of the rooms
    29.         gridSizeX = 80;
    30.         gridSizeY = 60;
    31.  
    32.         //Temporary
    33.         position = new Vector3(-0.79f, 0.59f, 0);
    34.  
    35.         CreateGrid();
    36.  
    37.     }
    38.  
    39.     void CreateGrid()
    40.     {
    41.  
    42.         //Fills the array for the graph
    43.         NodeGraph = new NodeScript[gridSizeX, gridSizeY];
    44.  
    45.         //Fills out the graph by first going through the X component of each Y component until it maxes out.
    46.         while (aphY < gridSizeY)
    47.         {
    48.  
    49.             while (aphX < gridSizeX)
    50.             {
    51.  
    52.                 position.x += .01f;
    53.                 if (Physics.CheckSphere(position, .01f) == true) { walkable = false; } else { walkable = true; }
    54.                 NodeGraph[aphX, aphY] = new NodeScript(walkable, position);
    55.                 aphX++;
    56.  
    57.                 if(walkable == false) { GameObject.Instantiate(placeholder); }
    58.  
    59.             }
    60.  
    61.             aphY++;
    62.             position.y -= 0.01f;
    63.             aphX = 0;
    64.  
    65.         }
    66.  
    67.     }
    68.  
    69. }
    70.  
    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)
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    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.
     
    SwaggyMcChicken likes this.
  3. SwaggyMcChicken

    SwaggyMcChicken

    Joined:
    Apr 13, 2015
    Posts:
    108
    Thanks! Can you explain why MonoBehaviors aren't allowed to be created using "new"? I don't understand.
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,380
    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).
     
  5. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,848
    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.
     
    djfunkey likes this.