I'm not sure what's going on. The error is coming from something like that very bottom line. I feel like I've tried a million different things and I'm at a loss. Any sort of data that I try to access from one of the members of my "dogButtonScriptArray" can be read just fine but when I try to copy it over to a local variable I get the error. Also btw, in the inspector, I can see clearly that each member of the array is filled correctly.

public class GameControl : MonoBehaviour {

public static GameControl control;

public int gold;
public DogSelectorButtonScript[] dogButtonScriptArray;
private bool[] unlockedDogsArray;

void Awake(){
    if (control == null){
        DontDestroyOnLoad (this);
        control = this;
    else if (control != this){
        Destroy (gameObject);
void OnLevelWasLoaded(){
    if (SceneManager.GetActiveScene ().buildIndex == 2) {
        dogButtonScriptArray = FindObjectsOfType <DogSelectorButtonScript> ();
        bool test = dogButtonScriptArray [2].isUnlocked; // this works fine
        unlockedDogsArray [1] = test; // error here
  • \$\begingroup\$ Null reference exception when happen that you have public thing that haven't initialize.in your code you should determine size of your arrays \$\endgroup\$ – Seyed Morteza Kamali Mar 15 '17 at 4:01

At no point in this code do you allocate unlockedDogsArray. You declare it near the top of your class, but you don't initialize it to anything, so the array is a null reference. You never do anything like unlockedDogsArray = new bool[size]" orunlockedDogsArray = someOtherArray` in the code you've posted.

Unless you do that elsewhere, the unlockedDogsArray remains null when you go to access it on the last line (unlockedDogsArray [1] = test) and thus you get a null reference exception.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.