0

So I've been trying to access game objects in my scene (which are disabled), to enable them. But I'm getting an error: "Object reference not set to an instance of an object"

private List<Character> characters = new List<Character>();
private List<GameObject> characterGameObjects = new List<GameObject> ();

public void loadSceneCharacters(){

    if (characters != null) {
        for(int i = 0; i < characters.Count; i++){
            characterGameObjects.Add(GameObject.Find(characters[i].CharacterName));
            characterGameObjects[i].SetActive(true);
        }
    }
}
Thanos Markou
  • 2,468
  • 3
  • 22
  • 31
Dannika Rodriguez
  • 131
  • 2
  • 3
  • 10
  • [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Jul 07 '15 at 10:43

4 Answers4

1

You can't find disabled gameobjects. A solution is to either reference them in inspector or find them all first when they are enabled, then disable those you don't need.

ElDuderino
  • 3,196
  • 2
  • 18
  • 35
0

I think your characters list is empty.

If you don't Instantiate GameObjects you can fill characters list with drag and drop so you can change code like this.

public List<Character> characters = new List<Character>();   ///Thanks to public 
                                              //you can see list in Unity Editor
private List<GameObject> characterGameObjects = new List<GameObject> ();

public void loadSceneCharacters(){

    if (characters != null) {
        for(int i = 0; i < characters.Count; i++){
            characterGameObjects.Add(characters[i]));  //get and add gameobject
            characterGameObjects[i].SetActive(true);
        }
    }
}

If you have dynamically created GameObjects you can fill the list with GameObject.Find("CharacterName");

However, i dont suggest that find every gameobject with name.Instead of that, During the Instantiate state you can add new gameObject instance to your character list.

Barış Çırıka
  • 1,534
  • 1
  • 15
  • 24
0

Even if the character list would be empty this code would not throw an exception.

The problem is probably that you can not find disabled gameobject using the find methods (at least that was my experience, correct me if i am wrong guys).

What i usually do as a workaround instead of searching is to add the gameobjects via drag and drop. If this is not possible you can either, search for the gameobjects in Awake or Start, add them to your list and disable them. Or do some sort of adding when you instanciate them... Basicly you have to somehow get the reference to the gameobjects before you disable them.

Hope this helps.

0

Something that can help you is to create an empty object, then put all your characters inside the empty object.. then by code do something like:

foreach(Character c in MyEmptyObject.GetComponentsInChildren(Character, true))//the true in this 
//line indicates that it should search for inactive objects
{
  c.gameObject.SetActive(true);
}

this assuming that your characters have an script called "Character"

Roberto Guajardo
  • 490
  • 4
  • 14