- Home /
[Solved] Change a bool of one of many instantiated clones
I have used the following code in my GameManager script to instantiate 20 clones of a prefab called Agent.
for (int i = 0; i < agentsTotal; i++)
{
Instantiate(agent);
agent.name = "Agent" + i;
}
I have used the agent.name line resulting in different names of the 20 clones.
I attached a script to the Agent prefab called Agent.
I have tried many things to change a bool of only one of the Agents. This should be done in the Start() part of the GameManager code but it looks like I am only able to change the bool in all clones or in no clone at all.
Can someone please point me into the right direction?
I thought it would be obvious that by changing the agent variable, u are changing the name of the agent prefab you assigned to it, and not the new instance of it. The Instantiate method returns a new gameobject instance, it is that one you should modify.
var agentInstance = Instantiate(agent);
Yeah, and thats where you mention the index, rather than doing same thing for each iteration
Thank you both for helping out. I will not make this mistake again :)
Answer by Vollmondum · Mar 23, 2020 at 07:57 PM
for (int i = 0; i < agentsTotal; i++)
{
var newInstance = Instantiate(agent, targetPositiin.position, Quaternion.identity);
newInstance.name = "Agent" + i;
if(i == "needed instance index") // set to an int you need
{
//newInstance whatever elase you wanna do with current "i" instance
}
}