I am doing a 2D game project in Unity5. Right now, I am having problem with setting my array of gameojects' active to false. I've attached this script to an object in my scene and attached 2 character gameobject to it. I have tried:
public GameObject[] charactersArray;
void Start ()
{
foreach (GameObject go in charactersArray)
{
go.SetActive(false);
Debug.Log(go.active);
}
}
Although the console output is false, but my gameobjects are still appearing inside my game.
So, I tried to see what's my gameobjects' active at the start:
public GameObject[] charactersArray;
void Start ()
{
foreach (GameObject go in charactersArray)
{
Debug.Log(go.active);
}
}
And the console output is still "false" for all my gameobjects.
Is there anything wrong with my codes? Am I missing out something?
This is happening because you are not modifying the GameObjects (Mr Knight and Ms Sorceress) instance in the scene. You are instead modifying the prefabs of both GameObjects.
Below is what your Objects looks like in the Editor which shows that you are using prefabs in the slots.
This is what the image should look like if you are using a non prefab object:
Simply drag the GameObjects (Mr Knight and Ms Sorceress) from the Hierarchy tab to the Element 0 and Element 1 respectively. This should work.
Note that if the Objects from the Hierarchy tab are also created from a prefab, you will see the first image above not the second one but you need to re-add them like I mentioned above because this looks like you have the prefabs in the slots instead of the actual objects in the scene that is in the Hierarchy tab.