• 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 Supertang · Sep 04, 2018 at 07:19 PM · listnullreferenceexception

List suddently becomes null

Hey, I'm trying to create some randomly generated rooms in a tilemap. I'm very early in the process and I only have 2 scripts. In my first script, I create a list called filledPos to store the cordinates of my existing rooms, but it keeps saying in my Detect-function that it is null!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Generator : MonoBehaviour {
 
     public List<Vector2> filledPos = new List<Vector2>();
     public GameObject room;
     public Vector2 maxSize;
     public int roomSize;
 
     void Start () {
         Spawn (maxSize / 2 * roomSize);
     }
 
     public void Spawn(Vector2 pos) 
     {
         Instantiate (room, pos, Quaternion.identity).GetComponent<SpawnRooms> ().Detect (maxSize / 2);
     }
 }

OTHER SCRIPT:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class SpawnRooms : MonoBehaviour {
 
     public Generator generator;
 
     void Start () {
         generator = GameObject.FindWithTag ("GameController").GetComponent<Generator> ();
 
     }
 
     public void Detect (Vector2 gridPos) 
     {
         //Spawn this room
 
         //Spawn Connected Rooms
         List<Vector2> checkPos = new List<Vector2>(){new Vector2 (gridPos.x + 1, gridPos.y), new Vector2 (gridPos.x, gridPos.y + 1), new Vector2 (gridPos.x - 1, gridPos.y), new Vector2 (gridPos.x, gridPos.y - 1)};
 
         for (int i = 0; i < checkPos.Count; i++) {
             print (checkPos [i]);
             if (!generator.filledPos.Contains(checkPos[i]))
             {
                 //generator.Spawn (checkPos[i]);
             }
         }
     }
 }

If I test for the List in Start() of any of the scripts, it works. But when I move into Detect() it's null. I've tried to create the list in Awake() and lot's of other things, but it does not make any difference.

The List is only used the times you see it here.

Comment
Add comment · Show 6
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 VitruvianStickFigure · Sep 04, 2018 at 07:34 PM 0
Share

Out of curiosity, you're certain that it's filledPos that is null? I see that generator is captured in a way that can make it null, and the error messages for accessing a null list (which would be weird for a $$anonymous$$onoBehavior) and a null component are very similar; is there a chance that Generator isn't attached (or yet attached) to GameController?

avatar image JVene · Sep 04, 2018 at 07:36 PM 0
Share

I don't see anything here that puts an entry into filledPos, where is that?

avatar image myzzie JVene · Sep 04, 2018 at 07:46 PM 0
Share

That list does not have to be filled. A list of 0 elements can still be checked if it contains something without throwing an exception.

avatar image JVene myzzie · Sep 04, 2018 at 07:53 PM 0
Share

One of the ways to figure out what's wrong is to find the place where items are added to the List - if the list ever goes null and that isn't failing, that may help localize where things are or are not going wrong.

Since I don't see that, I can't tell how the OP knows the list exists in the first place, or if the population of the list is even happening, and thus the first time the exception fires it trying to check an extant but empty list.

Show more comments

1 Reply

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

Answer by myzzie · Sep 04, 2018 at 07:52 PM

You are calling Detect() before Start() in SpawnRooms class. generator is therefor null when Detect is called. You can put generator = GameObject.FindWithTag ("GameController").GetComponent<Generator> (); inside Detect() to fix it.

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 Supertang · Sep 04, 2018 at 08:08 PM 0
Share

When you try your best but you don't succeed... I feel dumb.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

94 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

Related Questions

A node in a childnode? 1 Answer

How to null-reference check a Collider2D that's been destroyed 1 Answer

null reference exception when I already checked if this was null 1 Answer

Editor script executing before variable updated 1 Answer

Store Instantiated Object script reference 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