Multiplying the same enemy
I'm trying to create a function that will multiple my enemy by a chosen amount. I made this:
void DoubleTeam(int multipleAmount)
{
for(int i = 0; i <= multipleAmount; i++)
{
if(spawnedRight)
{
spawnedRight = false;
GameObject bossCopyPrefab = Instantiate(bossCopy, new Vector3(transform.position.x + positionAmountRight, transform.position.y, transform.position.z),Quaternion.identity) as GameObject;
multipliedSelfs[i] = bossCopy;
positionAmountRight++;
}
else if(!spawnedRight)
{
GameObject bossCopyPrefab = Instantiate(bossCopy, new Vector3(transform.position.x - positionAmountLeft, transform.position.y, transform.position.z),Quaternion.identity) as GameObject;
multipliedSelfs[i] = bossCopy;
positionAmountLeft++;
spawnedRight = true;
}
}
}
And I'm calling the function just for testing reasons like this:
case EyeBallStates.NonAggro:
if(!test)
{
DoubleTeam(2);
test = true;
}
break;
I'm not sure what I've done wrong but what the function is meant to do is spawn a copy of the enemy, move it to the right side and add it to an array. Then spawn another copy but move it to the left side and add that GameObject to the same array till it's spawned the chosen amount.
What it really does is keeps spawning copies to the right side past the chosen amount so it will make more then 30 copies or something when I only want two. It also doesn't seem to spawn them to the left side of the main enemy like it should, only the right side unless i set the spawnedRight variable to false.
Does anyone know why it's doing this?
So you want to create this effect: $$anonymous$$ey: * = Spawned enemy + = $$anonymous$$ain enemy A = Arrow to next step
(3) * + *
A
(2) + *
A
(1) +
For line 3, you may want to change that to:
for(int i = 0; i <= multipleAmount; i++) // From this
for(int i = 0; i < multipleAmount; i++) // To this
Furthermore, you may want to check if your function is called ONC$$anonymous$$ At times, I have encountered where my function was called multiple times due to placing it in the Update().