Accessing/Adding to a list from another script produces NullReferenceException

Greetings, I’ve run into some issues that I cant see to understand, hopefully someone will enlighten me :wink:

ScriptA, existing on 3-4 other objects, each holds its his own List.

using UnityEngine;
using System.Collections;

using System.Collections.Generic;

public class ScriptA : MonoBehaviour 
{

//declares the List, interacting with the List from within the script works just fine
 public List<string> List_StartingField = new List<string>();

Start () {}
Update(){} 

//etc.
//will later do something with the content of its List, not yet implemented since I have errors.

}

The ScriptB on a Manager Object which is supposed to Add something to the List_StartingField List in ScriptA

using UnityEngine;
using System.Collections;

using System.Collections.Generic;
using System.IO;

public class ScriptB : MonoBehaviour
{

ScriptA scriptA0;

void Start ()
{
         ScriptA scriptA0 = GameObject.Find("ScriptAs ObjectName").GetComponent<ScriptA>();
}


void AddSomethingToStartingFieldList()
{
    if (!scriptA0.List_StartingField.Contains(variableToAdd))                 //trying to access the List throws the NullReferenceErrors
                      scriptA0.List_StartingField.Add(variableToAdd);      
}


}

Everything compiles fine, but once I hit the Play button I’ll get a “NullReferenceException: Object reference not set to an instance of an object” to the console whenever I try to access the List from ScriptB, be it with Debug.Log or .Add().

ScriptA scriptA0 = GameObject.Find(“ScriptAs ObjectName”).GetComponent();
should just be
scriptA0 = GameObject.Find(“ScriptAs ObjectName”).GetComponent();

Otherwise you are creating a local instance only in that method, not setting the global instance of it.

gah!

Thanks, that did it.

I tested around for hours, rewriting stuff to narrow the issue down, but as it seems didnt try this one :wink:

Thank you so much! Been looking everywhere for this!