R
R
RandomProgrammer2021-03-15 11:44:13
C++ / C#
RandomProgrammer, 2021-03-15 11:44:13

Why is there no delay in my code and objects are created immediately?

No delay, objects are created immediately.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class cube : MonoBehaviour
{
    // Start is called before the first frame update
    public GameObject obj;
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey(KeyCode.F))
        {
            StartCoroutine(inst());
        }
    }

    IEnumerator inst()
    {
        while (true)
        {
            Instantiate(obj, new Vector3(0f, Random.Range(-10f, 10f), 0f), Quaternion.identity);
            yield return new WaitForSeconds(1.0f);
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
KoreanGuy, 2021-03-15
@KoreanGuy

you have yield return after Instantiate. Just swap.

R
RandomProgrammer, 2021-03-15
@RandomProgrammer

Input.GetKeyUp

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question