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());

    }
    
    

}
ina
  • 19,167
  • 39
  • 122
  • 201

2 Answers2

1

Unity C# 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);

(Minor Note that Random.Range is inclusive vs exclusive depending on whether you use integers or floats.)

Similarly when you define a Vector2 bob = new Vector2(0.5f,0);, the f is also needed to denote explicitly that it is a float.

ina
  • 19,167
  • 39
  • 122
  • 201
  • `Unity requires that you specify whether your decimal is specifically a float or a double` .. no this is `c#` in general ;) `0.2` is a `double`, `0.2f` is a `float` ... Unity's `Random.Range` however requires either `int` or `float` values and OP was passing in `int` => the result will be `int` between `1` and `2-1` ... not many options there ;) – derHugo Feb 22 '22 at 08:22
  • updated. c# though specifically has you use different functions for floats vs doubles https://learn.microsoft.com/en-us/dotnet/api/system.random – ina Feb 22 '22 at 08:37
  • OP is using [`UnityEngine.Random.Range`](https://docs.unity3d.com/ScriptReference/Random.Range.html) .. but the parameters OP is passing in are `c#` types .. that's no really Unity specific .. it is not about `double` vs `float` as you say but about `float` vs `int` – derHugo Feb 22 '22 at 08:40
  • Hmm, if you just throw in decimals without adding a `f`, it wouldn't work either... – ina Feb 22 '22 at 08:46
  • it wouldn't be a `decimal` then .. but a `double` ^^ But OP didn't even try that ;) – derHugo Feb 22 '22 at 08:48
0

Random.Range has two overloads.

public static float Range(float minInclusive, float maxInclusive); 

and

public static int Range(int minInclusive, int maxExclusive);

you are passing in

Random.Range(1, 2);

which are two int values so the second overload is used where the result will be an int between 1 and 2 - 1 ... not many options here ;)

Also your attempt

Random.Range(1/2, 2);

are again int values! 1/2 is an integer division which results in 0! So this random can either return 0 or 1.


What you rather want to do is passing in float values like e.g.

Random.Range(1f, 2f);

which can result in any floating point value between 1 and 2 or accordingly

Random.Range(0.5f, 2);

or back to your attempt

Random.Range(1 / 2f, 2);

which now uses a float division and thereby the result is automatically a float so the first overload will be used.


In general btw there is no need to call a Coroutine recursively, you can simply loop forever and also note that Start itself can be a Coroutine like e.g.

private IEnumerator Start()
{
    while(true)
    {
        yield return new WaitForSeconds(Random.Range(0.5f, 2f)); 
    
        Instantiate(astriod, new Vector2(Random.Range(-xBounds, xBounds), yBounds), Quaternion.identity);
    }
}
derHugo
  • 83,094
  • 9
  • 75
  • 115
  • consider using async void – ina Feb 22 '22 at 08:42
  • @ina no, why would you do that? Most o the Unity API is explicitly only allowed to be used on he Unity main thread .. introducing async will come with a lot of side effects. Also `async void` is something to generally [avoid](https://stackoverflow.com/questions/70660049/how-do-i-avoid-async-void) ;) – derHugo Feb 22 '22 at 08:49