Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How do you speed up WaitForSeconds in a coroutine when you want the number to be less than 1?

I am trying to create a program that will spawn balls from the top randomly at random times. The problem is it is not fast enough, but if I change the value to like 1/2 it spawns 50 super fast.

using System.Collections.Generic;
using UnityEngine;

public class SpawnAstroids : MonoBehaviour
{
    public GameObject astriod;
    public float xBounds, yBounds;
    public int playerPoints = 0;
    public int enemyPoints = 0;

    void Start()
    {
        StartCoroutine(SpawnRandomGameObject());
        
    }

    IEnumerator SpawnRandomGameObject()
    {
        yield return new WaitForSeconds(Random.Range(1,2)); //Random.Range(1/2, 2)
        
        Instantiate(astriod, new Vector2(Random.Range(-xBounds, xBounds), yBounds), Quaternion.identity);

        StartCoroutine(SpawnRandomGameObject());

    }
    
    

}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Unity requires that you specify whether your decimal is specifically a float or a double. Add in f to the end of each decimal number. For example: Random.Range(0.5f, 2);

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading