Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Mithrilshadow · Feb 27, 2013 at 06:06 AM · gameobjectlistnullreferenceexception

Can't add GameObjects to a List

I am writing a dungeon generator. Its in very early stages. I got it to detect 1 exit node and spawn a hall tile and a room tile beyond that. An exit node is an empty GameObject. Now I want to make a list to store more then one exit node. I can then later iterate through all the nodes and spawn halls and rooms. I am stuck at this error:

NullReferenceException: Object reference not set to an instance of an object Dungeon.AddNodes (UnityEngine.Transform nodes) (at Assets/Dungeon.cs:47) Dungeon.FindNodes () (at Assets/Dungeon.cs:41) Dungeon.Start () (at Assets/Dungeon.cs:19)

Here is the code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class Dungeon : MonoBehaviour {
     
     public GameObject start;  //holds the start tile
     public GameObject player; //holds the player pawn
     public GameObject halls;  //holds the hall tiles, will be changed to an array later.
     public GameObject rooms;  //holds the room tiles, will be changed to an array later.
     
     
     private List<Transform> exitNodes; //holds the current nodes, will be updated after each iteration of room generation.
     
     
     
     // Use this for initialization
     void Start () {
         CreateStart();
         FindNodes();        
         SpawnDungeon();
     }
     
     // Update is called once per frame
     void Update () {
     
     }
     
     private void CreateStart()
     {
         //creates the starting tile and player pawn.
         GameObject go = Instantiate(start, Vector3.zero, Quaternion.identity) as GameObject; 
         GameObject pc = Instantiate(player, Vector3.up, Quaternion.identity) as GameObject;
     }
     
 
     
     private void FindNodes()
     {
         //Finds all nodes tagged for the north side of the tile, indicating a positive x translation.
         GameObject[] node = GameObject.FindGameObjectsWithTag("N_exit");
         foreach(GameObject temp in node)
         {
             // adds the nodes to exitNode list, not working...
             exitNodes.Add(temp.transform);
         }
     }
     
     
     
     private void SpawnDungeon()
     {
         // Vector3.zero will become the location of the current node.
         GameObject spawn = Instantiate(halls, Vector3.zero, Quaternion.identity) as GameObject;
         //translate to the proper offset.
         spawn.transform.Translate(2.5f,0,0);
         //Vector3.zero will become the location of the current node.
         spawn = Instantiate(rooms, Vector3.zero, Quaternion.identity) as GameObject;
         //translate to the proper offset.
         spawn.transform.Translate(10,0,0);
     }
 }
 
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

2 Replies

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

Answer by appearance · Feb 27, 2013 at 06:22 AM

You have only declared the list but not instantiated it.

You can instantiate the List in Start () method:

  void Start () {
     exitNodes = new List<Transform> ();
     CreateStart();
     FindNodes();
     SpawnDungeon();
 }
Comment
Add comment · 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
0

Answer by Mithrilshadow · Feb 28, 2013 at 03:01 AM

Doh! I knew it was going to be a n00b mistake! Thanks a bunch.

Comment
Add comment · Show 1 · 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 Halleflux · Apr 13, 2014 at 05:57 AM 1
Share

Post comments as comments, not new answers please.

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

11 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

Related Questions

A node in a childnode? 1 Answer

NullReferenceException in follow function 0 Answers

Adding a prefab to gameObject at a certain position in runtime 0 Answers

How to use if statement properly 1 Answer

Find specific prefab in a list 2 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