- Home /
Agent killing and respanwning
I have 10 Agents in my scene and there are fighting 5 vs. 5 with guns. Im working with the ML-agents toolkit and I have to train these agents. I have a timer which counts from 60 to 0 and if the time is up all agents have to respawn and set to their original positions. But it is not working. Here is my code for killing and respawning the agents:
//list for the 10 agents
List<GameObject> listOfAgent = new List<GameObject>();
List<GameObject> listOfCounterAgent = new List<GameObject>();
//list for killed agents
List<GameObject> removeListOfAgent = new List<GameObject>();
List<GameObject> removeListOfCounterAgent = new List<GameObject>();
private void Awake
{
//Inserting all agents to list
listOfAgent.AddRange(GameObject.FindGameObjectsWithTag("agent"));
Debug.Log("Anzahl der Agenten: "+ listOfAgent.Count);
listOfCounterAgent.AddRange(GameObject.FindGameObjectsWithTag("counterAgent"));
Debug.Log("Anzahl der CounterAgenten: " + listOfCounterAgent.Count);
}
//Removing the agents from the list if they are dead and inserting them to removeList
public void AgentDead(GameObject opponent)
{
if (listOfAgent.Contains(opponent))
{
listOfAgent.Remove(opponent);
removeListOfAgent.Add(opponent);
}
Debug.Log(listOfAgent.Count + " Agenten sind übrig");
}
//here the same
public void CounterAgentDead(GameObject opponent)
{
if (listOfCounterAgent.Contains(opponent))
{
listOfCounterAgent.Remove(opponent);
removeListOfCounterAgent.Add(opponent);
}
Debug.Log(listOfCounterAgent.Count + "Counter Agenten sind übrig");
}
//check if all agents are dead
public bool AreAgentsDead()
{
if (listOfAgent.Count <= 0)
{
// Alle Agenten sind tot!
return true;
}
else
{
// Sie leben noch!
return false;
}
}
public bool AreCounterAgentsDead()
{
if (listOfCounterAgent.Count <= 0)
{
// Alle Counter Agenten sind tot!
return true;
}
else
{
// Sie leben noch!
return false;
}
}
//if time is up, then call enemy and counterEnemy done
public void TimeIsUpGM()
{
//time is at 0
EnemyDone();
CounterEnemyDone();
}
public void EnemyDone()
{
//set all agents to active
SetEnemysActive();
//listOfAgent.Clear();
//removeListOfAgent.Clear();
//add removed agents to original list
for (int i = 0; i < removeListOfAgent.Count; i++)
listOfAgent.Add(removeListOfAgent[i]);
Debug.Log("Anzahl der Agenten: " + listOfAgent.Count);
//set agents to done
for (int i = 0; i < listOfAgent.Count; i++)
listOfAgent[i].GetComponent<EnemyController>().Done();
}
public void CounterEnemyDone()
{
//SetCounterEnemiesNotActive();
SetCounterEnemysActive();
//listOfCounterAgent.Clear();
//removeListOfCounterAgent.Clear();
for(int i = 0; i < removeListOfCounterAgent.Count;i++)
listOfCounterAgent.Add(removeListOfCounterAgent[i]);
Debug.Log("Anzahl der Counter Agenten: " + listOfCounterAgent.Count);
for (int i = 0; i < listOfCounterAgent.Count; i++)
listOfCounterAgent[i].GetComponent<CounterEnemyController>().Done();
}
public void SetEnemysActive()
{
for (int i = 0; i < removeListOfAgent.Count; i++)
removeListOfAgent[i].SetActive(true);
}
public void SetCounterEnemysActive()
{
for (int i = 0; i < removeListOfCounterAgent.Count; i++)
removeListOfCounterAgent[i].SetActive(true);
}
But this code is not working. When the time is up and no agent has died, then everything is normal. Count of agents and counterAgents are 5. But if someone dies and the time is up, then the died agent won't revive. It is so confusing. The counter goes then up to agent: 5 and counterAgent: 6 then next round agent:5 counterAgent:7
And the Done method is in an other class and calls AgentReset()
public override void AgentReset()
{
gameObject.transform.position = spawnPoint.position;
agentRB.velocity = Vector3.zero;
agentRB.angularVelocity = Vector3.zero;
}
I don't know how to solve this
Your answer
![](https://koobas.hobune.stream/wayback/20220612201028im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
A node in a childnode? 1 Answer
unity3d multiplayer respawn 2 Answers
Respawning player not working 2 Answers
Respawning gameobject not working c# 0 Answers
2D Platformer - How do I have objects that kill the player upon touch/cause respawn? 2 Answers