items not adding to list
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.
Answer by AurimasBlazulionis · Mar 20, 2017 at 12:23 PM
Your node class is derriving from MonoBehaviour, remove : MonoBehaviour
from the class definition.
Also the line in the second script should be myTown.AddToResList(node, res);
.
but the second script is what's sending the information that needs to be passed into the list, should I put that function into the second script ins$$anonymous$$d of passing it through via an add function?
Thank you so much, removing the $$anonymous$$onoBehaviour fixed the issue, and after fixing a few other hickups it works perfectly, thank you so much. It's always the little things that will trip you up the hardest lol.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113542im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Finding the max Y position value in a list of game objects and assigning it to a variable? 0 Answers
item not adding to list correctly! 1 Answer
Cannot figure out error NullReferenceException: Object reference not set to an instance of an object 1 Answer
Updating UI Text of an existing element in a list. 0 Answers