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 /
avatar image
0
Question by vanessasawr · Mar 03, 2018 at 12:11 PM · errorbugnullreferenceexceptiondebugging

Why am I getting NullReferenceException on my .Contains()?

Unity and C# newbie here.
I have created a list in the script GameInformation:
public class GameInformation : MonoBehaviour{ public static List inventory; }
I have then tried to use the inventory.Contains("string") command in a different script:
public class Interaction_Object : GameInformation { void Update() { if (inventory.Contains("Key")==true) { gameObject.SetActive(false); } } }
I am using the following libraries:
using System.Collections; using System.Collections.Generic; using UnityEngine;
I am getting a NullReferenceException error on the .Contains() command, I have tried removing the ==true and that did nothing, also tried populating my list only to get a plethora of errors.

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 Xarbrough · Mar 03, 2018 at 12:22 PM

Have you initialized your list correctly?

 public static List<string> myList = new List<string>();

Before you can use a list, it must be instantiated first. If the (generic) list or array is serializable, not static and public or marked with the SerializeField attribute, Unity will show it in the inspector. This also means Unity takes care of instantiating it for you, if you don't do it. However, a static variable will not show in the inspector and Unity doesn't do anything with it, which is why you have to create it yourself.

Not sure, if only a typo, but you should be using the generic versions of all collections (the ones with -brackets).

Another side-note: I wouldn't recommend the use of static variables as a means of communicating between scripts. :p Statics have a very specific use-case for data which is shared between all instances of a class or doesn't belong to any instance at all. They have many gotchas and require some experience from programmers using them. You could run into a lot of problems like references to already destroyed objects in your list, or preventing garbage collection. And of course the simple fact that you need to manage the lifecycle of a static list entirely by yourself, e.g. creating it, removing invalid reference in OnDisable or OnDestroy and so on. Better use a public instance variable and find your object via FindObjectOfType or via an inspector reference.

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

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

106 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

Related Questions

3rd party bug reporting system similar to unity's "Report A bug" 3 Answers

"Build failed. Method not found: 'Int32' 0 Answers

Error with Update and OntrrigerEnter funtion 1 Answer

Why does unity3D crash when starting this script in play mode? 1 Answer

when pressing play gameobject get deleted and the game breaks 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