Reputation: 1
The project is made in Unity 2D.
I am working on a unity Project where I respawn my items, after the player ran through the level. When he does not have all the emeralds he will respawn at the start of the level and I want Items to respawn for him.
So for now I am using:
void OnCollisionEnter2D (Collision2D col)
{
if (col.gameObject.CompareTag("Life"))
{
col.gameObject.SetActive(false);
}
}
The Objects are inside an empty Object for clarification. I make them the child of the empty Object "Items".
if (ManagerVariables.IsRespawning)
{
if (item!= null)
{
for (int a = 0; a < item.transform.childCount; a++)
{
item.transform.GetChild(a).gameObject.SetActive(true);
}
}
else
{
Debug.LogError("No Objects Over Class defined");
}
I already checked for the variable IsRespawning and it is set to true when the player respawns or dies, but the objects do not reappear. As well I want to ask about object with multiple child objects how to do that e.g. Enemy with FirePoint attached
Upvotes: 0
Views: 113
Reputation: 78
The code should be working generally. My suggestion is to check whether item has a value. Maybe item is always null and your loop never gets executed. If item is not null I would check the child count and make sure it is larger than 0.
Regarding your question with multiple child objects, you could either disable every child individually or disable the parent, then all the children also disabled.
Upvotes: 1