- Home /
Want to spawn an object with button press, multiple times that gets higher each time?
So I want a Turret to spawn on top of another Turret, but if I do this For Loop, it just spawns however many times, per button press. I want it to spawn once when button pressed, then when the button is pressed again I want it to spawn in a higher location than the previous one.
for (int i = 0; i < 1; i++)
{
GameObject spawnTurret = Instantiate(turret, new Vector3(0, i * 4, 0), Quaternion.Euler(0, 180, 0));
spawnTurret.transform.localScale = new Vector3(9, 9, 9);
}
Answer by ozrfrkan · Oct 14, 2020 at 12:14 PM
public GameObject Turret;
private Vector3 TurretPos;
public void InstantiateTurret()
{
Instantiate(Turret, TurretPos, Quaternion.Euler(TurretPos));
TurretPos.y += Turret.transform.localScale.y;
}
By this script you will instantiate a tower and then after this tower new y axis of turret position will increase by scale of turret object.
Give your buttons onClick event this method, give the turret prefab also and it will work.
Your answer
Follow this Question
Related Questions
How to make a gameObject instantiate a prefab for every 250 damage taken ? 4 Answers
Distribute terrain in zones 3 Answers
Order of instantiated objects 0 Answers
Order of instantiated objects in hierachy 3 Answers
Multiple Cars not working 1 Answer