I’m a complete noob to coding, and I feel like I’m missing a very basic concept, and/or messing up something that should be very simple. here’s the code in question.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable]
public class TownEcoHandler : MonoBehaviour
{
public List<Node> nodes = new List<Node>();
public void AddToResList(GameObject resNode, string resType)
{
GameObject node = resNode ;
nodes.Add(new Node(resNode, resType));
}
}
[System.Serializable]
public class Node : MonoBehaviour
{
GameObject node;
int nodeid = 0;
Vector2 loc;
string resource;
public Node(GameObject newNode, string newRes)
{
TownEcoHandler tc = GetComponent<TownEcoHandler>();
node = newNode;
loc = node.transform.position;
nodeid = tc.nodes.Count + 1;
resource = newRes;
node.GetComponent<ResNodeHandler>().nodID = nodeid;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ResNodeHandler : TownEcoHandler {
public string res;
public GameObject node;
public TownEcoHandler myTown;
public int nodID;
//public Count iron;
void Start()
{
node = this.gameObject;
myTown = GetComponentInParent<TownEcoHandler>();
AddToResList(node, res);
}
public void ResourceTick ()
{
}
}
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
UnityEngine.MonoBehaviour:.ctor()
Node:.ctor(GameObject, String) (at Assets/Scripts/Economy/TownEcoHandler.cs:29)
TownEcoHandler:AddToResList(GameObject, String) (at Assets/Scripts/Economy/TownEcoHandler.cs:15)
ResNodeHandler:Start() (at Assets/Scripts/Economy/ResNodeHandler.cs:21)
NullReferenceException
UnityEngine.Component.GetComponent[TownEcoHandler] () (at C:/buildslave/unity/build/artifacts/generated/common/runtime/ComponentBindings.gen.cs:45)
Node…ctor (UnityEngine.GameObject newNode, System.String newRes) (at Assets/Scripts/Economy/TownEcoHandler.cs:31)
TownEcoHandler.AddToResList (UnityEngine.GameObject resNode, System.String resType) (at Assets/Scripts/Economy/TownEcoHandler.cs:15)
ResNodeHandler.Start () (at Assets/Scripts/Economy/ResNodeHandler.cs:21)
instead of getting the node to add to the list, what I get are these errors, and I have no clue what I’m doing wrong, I hate coming to others for help most the time, but apparently my “learn on your own” method isn’t working well with “lists”… I don’t get along well with lists lol.