B
B
Bruh_Bruh2020-10-03 14:53:37
Unity
Bruh_Bruh, 2020-10-03 14:53:37

Why does object spawning work incorrectly?

Hello everyone, I'm trying to spawn objects when the character is on the ground. I'm trying to implement this through "Coroutine", but the objects spawn very quickly, a few per second, I think. The time limiter is worth it. What to do?

public GameObject enemy;
    public PlayerMovement PlayerMovementScript;
    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
         StartCoroutine(da());
    }

    IEnumerator da()
    {
        while (PlayerMovementScript.isGrounded == true)
        {
            Instantiate(enemy, new Vector3(Random.Range(-30, 30), 1f, Random.Range(-30, 30)), Quaternion.identity);
            yield return new WaitForSeconds(3f);
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2020-10-03
@GavriKos

And why are you starting a coroutine in an update? You have her START run every frame.
The coroutine needs to be started ONCE - it will wrap itself in an update

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question