0

I've created a list in a unity script called "Menu Selection" that's attached to a game object called "MenuScene." In that script, I made a list called "items" that I want to reference in another script. This is a snip of what I have, but I keep getting the error, "object reference not set to an instance of an object unity."

Here is part of the Menu Selection Script

public List<string> items;

void Update () {
    Debug.Log(items.Count);
}

public void onCoconutClick()
{
    items.Add("coconut");
}

And here's the script where I'm trying to use the "items" list

public List<string> list ;
public string name;

// Use this for initialization
void Start () {
    OGMat = GetComponent<Renderer>().material;
    list = GameObject.Find("MenuScene").GetComponent<MenuSelection().items;

}

Anyone know what's going on?

  • 1
    I see the *declaration* but not the *initialization* of `items`. Maybe you need to change to `public List items = new List();` – John Wu Apr 03 '18 at 00:15
  • 2
    Initialize `items` in the first script in the `Awake` function. This makes sure that it will be initialized before it is used in the `Start` function from the other script. – Programmer Apr 03 '18 at 00:24

0 Answers0