Hi there,
Im working on my first Unity project and hit a certain issue. I need to spawn a gameobject only Once when the timer reaches 0. My script spawns it nonstop. What am i doing wrong here?
Thanks.
{
[SerializeField] float timer = 6f;
public GameObject catEnemy;
void Start()
{
}
private void FixedUpdate()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
Instantiate(catEnemy, transform.position, transform.rotation);
}
}
}
You can use a bool hasSpawn and add it to your conditions.
if (!hasSpawned && timer <= 0)
{
timer = 6;
Instantiate(catEnemy, transform.position, transform.rotation);
hasSpawned = true;
}
private void FixedUpdate()
{
timer -= Time.deltaTime;
if (timer <= 0)
instantiator();
}
public void instantiator()
{
Instantiate(catEnemy, transform.position, transform.rotation);
///Set the timer back to original value
timer+=6f;
}
You are not resetting your timer. Once it reaches 0 it will start executing the code inside the “if” statement constantly.
if (timer <= 0)
{
timer = 6;
Instantiate(catEnemy, transform.position, transform.rotation);
}