References to instantiated gameobject are there but throwing a NullReference error regardless.

Well, I’m stumped. I have spent hours and hours researching and trying various options to no avail.

I have the following code:


using System.Collections.Generic;
using UnityEngine;
using System.Linq;

public class ButtonActionManager : MonoBehaviour
{
    //Array of GameObjects in the Resources folder.
    [SerializeField]
    private GameObject[] windows;

    //State of the windows - either open or closed
    [SerializeField]
    private List<string> windowStates = new List<string>();

    //Array of the instances (clones) which can be instantiated when the button is clicked
    [SerializeField]
    private List<GameObject> instances = new List<GameObject>();

    //The GameObject that will be the parent of the instantiated window
    private GameObject windowParent;
    
    //The location wwhere the windoww will be instantiated when the button is clicked.
    [SerializeField]
    private Vector3 homeLocation;
    

    void Start()
    {
        windowParent = GameObject.FindGameObjectWithTag("WindowsParent");
        homeLocation = new Vector3(0, 0, 0);
        windows = Resources.LoadAll("PageFlowPrefabs/WindowPrefabsToLoad", typeof(GameObject)).Cast<GameObject>().ToArray();

        for (int i = 0; i < windows.Length; i++)
        {
            //Adds the string "closed" to the list for each window in the windows array
            windowStates.Add("closed");
            //Adds an instance of each of the GameObjects(windows) in the windows array to the instances list
            instances.Add(windows*);*

}
}

public void OpenWindow_1()
{
if (windowStates[0] != “open”)
{

//clone and instance of the the prefab
instances[0] = Instantiate(windows[0], homeLocation, Quaternion.identity) as GameObject;
Debug.Log(instances[0]);

//Set the parent of the cloned instance in the scene. This will allow setting the sibling in the drag script so that the selected window is always on top.
instances[0].transform.SetParent(windowParent.transform);

//instances[0].transform.SetAsFirstSibling();
windowStates[0] = “open”;
}
else
{
Destroy(instances[0], 0f);
windowStates[0] = “closed”;
Debug.Log(windowStates[1]);
}
}

public void OpenWindow_2()
{
if (windowStates[1] != “open”)
{
instances[1] = Instantiate(windows[1], homeLocation, Quaternion.identity) as GameObject;
instances[1].transform.SetParent(windowParent.transform);
windowStates[1] = “open”;
}
else
{
Destroy(instances[1], 0f);
windowStates[1] = “closed”;
}
}
}
----------
Here is what this does There is an array called “windows” which is set in Start() by Resources.LoadAll.
All of the fields are serialized private variables. In the inspector, before clicking play, they show size 0 and nothing is listed. This is as expected. Here is an image:
----------
[166847-1.png|166847]*
----------
After I click play the Inspector looks like this:
----------
[166848-2.png|166848]*
----------
When I Click the button for Chapter 1 (method = “public void Open)1()”) it does instantiate a clone of the prefab from the resources folder. In the inspector, prefab in the instances list changes to a clone of the prefab. All good so far. But then, an error comes up as well and the code stops running when it gets to the line in that method which reads:
----------
instances[0].transform.SetParent(windowParent.transform);
----------
At that point, because execution has stopped due to the error, the WindowsStates list is not updated to show that the prefab is “open”. So everything get weird then, if I click the button again it opens another instance of the prefab instead of destroying it.
----------
I placed a Debug.Log(instances[0];
----------
The console does indeed show that the first item in the instances list is the prefab which instatiates when i click the button. The very next line of code (the one shown above where the error comes up give me this error in the console on play mode:
----------
NullReferenceException: Object reference not set to an instance of an object
ButtonActionManager.OpenWindow_1 () (at Assets/PageFlow/Scripts/ButtonActionManager.cs:55)
----------
By the way the same sequence of event occurs in the method “OpenWindow_2()”.
----------
Like I said, I have been banging my stupid head on this for hours and have not found an answer. What I am trying to achieve is to have the arrays and lists populated based on the contents of the resources folder. I could potentially have hundreds of these windows in the game. I want to be able to destroy them when the button is clicked a second time (and any child objects along with them) instead of simply disabling them. Also, out of laziness, to some degree, I do not want to have to drag and drop all these prefabs into a public array in the inspector.
----------
Incidentally, if I refactor and make the windows array public and drag and drop the prefabs in everything works like a charm!
----------
Any answers are much appreciated. Thanks in advance!
*
*

My first idea is, that the “windowParent” hasn’t been assigned so make it public and look for yourself. Since it doesn’t throw an error, when he won’t find the object with that tag, you won’t realize, if it’s assigned unless you see it for yourself. Also you can just assign the parent of the instantiated objects after the Quaternion.identity! So type in Instantiate(Position, Rotation, Parent);.