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.

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. :stuck_out_tongue: 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.