Roll a Ball Game Tutorial: Reactivate the Pick Up Objects?
Hello, I did this tutorial: http://unity3d.com/learn/tutorials/projects/roll-ball-tutorial
All is working fine.
Now I wanted to extend the game by resetting everything (without loading scene again). I can reset the player fine, but I have problems with the Pick Ups gameobjects. I searched for similar problems here and found this solution, which seems good:
GameObject[] gameObjects = GameObject.FindGameObjectsWithTag("Pick Up");
foreach (GameObject item in gameObjects)
{
item.SetActive(true);
}
But it doesn't work. The Array is empty (tried to use index 1 for example) but the Tag is right, because I also used it for the collision detection. Whats wrong?
(btw: Or is it also possible to speak with the parent of all the Pick Up objects?)
Thanks for help.
Answer by SpaceManDan · Sep 27, 2015 at 07:22 AM
Your problem is the way you are updating the array. When you are filling up your array it will not find inactive objects so what you need to do is this.
GameObject[] gameObjects;
void Start()
{
gameObjects = GameObject.FindGameObjectsWithTag("Pick Up");
}
void Update()
{
if (Input.GetKeyDown("g"))
{
foreach (GameObject item in gameObjects)
{
item.SetActive(true);
}
}
}
You need to fill the array at the beginning of the game and no other time. You want it to remain filled with all the objects, not updated every time you want to reset.
Thanks, that helped. So the problem was, that the FindGameObjectsWithTag("Pick Up")-function can't find inactive GameObjects :/ I already tried the thing with the start a while ago, but the compiler said, that I couldn't use that function in the start... but maybe I had some other bugs at that time, too... Well, now its working, so thanks again^^