Search Unity

Resolved if statement and Random.Range not working together

Discussion in 'Getting Started' started by Gummi_Ghost, Nov 23, 2021.

  1. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    I know that ints are exclusive when using Random.Range, but even though I put
    Code (CSharp):
    1. (spawnAttempt == (maxSpawnRate - 1))
    and made maxSpawnRate a high number, the console is still flooded with
    Code (CSharp):
    1. A new object has been created.
    . Should I make the
    Code (CSharp):
    1. maxSpawnRate
    higher, use floats instead of ints, or use a courotine or a different kind of update? Nothing I do works, I would appreciate any help.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SkyGeneration : MonoBehaviour
    6. {
    7.     //used with random.Range in attemptSpawn(); to control the probability of the object appearing
    8.     public int maxSpawnRate = 500;
    9.     public int spawnAttempt;
    10.  
    11.     void Update()
    12.     {
    13.         attemptSpawn();
    14.     }
    15.  
    16.     void attemptSpawn()
    17.     {
    18.         spawnAttempt = Random.Range(0, maxSpawnRate);
    19.         if (spawnAttempt == (maxSpawnRate - 1))
    20.         {
    21.             spawn();
    22.         }
    23.     }
    24.  
    25.    void spawn()
    26.   {
    27.     Debug.Log("A new object has been created.");
    28.   }
    29. }
     
    Last edited: Nov 23, 2021
  2. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Well, If you leave this script running long enough, it may eventually execute your
    spawn
    method, it's just that the likelihood of the method executing after picking a random number between 0 & 500 is about a 0.2% chance.

    Maybe something like this is what you're looking for?
    Code (CSharp):
    1. public class Example : MonoBehaviour
    2. {
    3.   [Range(0f, 1f)]
    4.   public float spawnChance;
    5.  
    6.   void AttemptSpawn()
    7.   {
    8.     if(Random.value < spawnChance)
    9.     {
    10.       Spawn();
    11.     }
    12.   }
    13. }
    Random.value generates a float between 0 & 1 inclusively. The higher
    spawnChance
    is, the more likely the
    Spawn
    method will execute.
     
    Gummi_Ghost likes this.
  3. Gummi_Ghost

    Gummi_Ghost

    Joined:
    Sep 21, 2020
    Posts:
    35
    Hello, thank you for answering. I'll be sure to give it a try.
     
  4. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,512
    Yeah, this is sort of inverted from how I'd do this. You may want to consider using a coroutine instead of blasting away in the Update method as well.

    Note this is untested code, but it should give you an idea of a potentially better direction.

    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class RandomSpawn : MonoBehaviour
    5. {
    6.     private bool doSpawn;
    7.     private float minSpawnTime = 5f;
    8.     private float maxSpawnTime = 15f;
    9.  
    10.     void Start()
    11.     {
    12.         StartCoroutine(Spawn());
    13.     }
    14.  
    15.     private IEnumerator Spawn()
    16.     {
    17.         if (doSpawn)
    18.         {
    19.             // Do spawn code here
    20.  
    21.             // Setup for next spawn
    22.             yield return new WaitForSeconds(Random.Range(minSpawnTime, maxSpawnTime));
    23.             StartCoroutine(Spawn());
    24.         }
    25.     }
    26. }
    27.