- Home /
SetActive(true) is not working
I have a game where bots are fighting 5 vs 5. To manage the death and respawning, I have two lists. One is to fill it with the agents. If one agent dies it will be removed from the list and is inserted to the removeList. The agent is not active then. Everything works fine. Then if I want to respawn them and setActive(true) it is not working. The most of the things are done in the class GameManager. For example the method for filling the removeList:
public void CounterAgentDead(GameObject opponent)
{
if (listOfCounterAgent.Contains(opponent))
{
listOfCounterAgent.Remove(opponent);
removeListOfCounterAgent.Add(opponent);
opponent.SetActive(false);
}
}
I think the main problem is here, because I am calling this method from an other class and give the gameobject parameter. And in the GameManager class Im filling the agent list: listOfAgent.AddRange(GameObject.FindGameObjectsWithTag("agent"));
And I try to activate it with this code:
public void SetEnemysActive()
{
int count = 0;
//for (int i = 0; i < removeListOfAgent.Count; i++)
// removeListOfAgent[i].gameObject.SetActive(true);
foreach (GameObject obj in removeListOfAgent)
if (!obj.activeSelf)
{
count++;
Debug.Log("Deactivated Agents: "+count);
obj.SetActive(true);
}
}
So I have the CounterAgentDead method with its own gameobject parameter saved in the list and on the other side the list which is filled with the agents. I think this causes the problem, because they have different references and therefore I can't activate it. Am I right? Can someone help me with this?
I guess the problem is that your are trying to find the inactive GameObjects with GameObject.FindGameObjectsWithTag
. The function "Returns a list of active GameObjects tagged tag". If your agent GameObjects are not active, they don't make it into the list.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
physics.OverlapSphere colliders 1 Answer
Quickly access specific list elements in a constantly changing list 1 Answer
Agent killing and respanwning 0 Answers