Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Valnor · Mar 20, 2017 at 11:45 AM · scripting problemlistslearning

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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);.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Valnor · Mar 20, 2017 at 01:53 PM 0
Share

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?

avatar image Valnor · Mar 20, 2017 at 02:44 PM 0
Share

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.

avatar image AurimasBlazulionis Valnor · Mar 20, 2017 at 06:40 PM 0
Share

do not forget to accept the answer.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

121 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding the max Y position value in a list of game objects and assigning it to a variable? 0 Answers

How can I filter a list of scriptable objects by an enum type and then pick a random index from the filtered list? 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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges