Search Unity

How to spawn at random times?

Discussion in 'Getting Started' started by kmo86, Feb 19, 2021.

  1. kmo86

    kmo86

    Joined:
    Dec 15, 2020
    Posts:
    104
    I'm working on the Challenge 2 - Play Fetch - Unity Learn On the 1st bonus question it says spawn balls at random times between 3 and 5 seconds. I have tried putting the Random.Range in the float and get error messages every time. So I tried in the start method and get no error messages.
    Code (CSharp):
    1.  
    2. public class SpawnManagerX : MonoBehaviour
    3. {
    4.     public GameObject[] ballPrefabs;
    5.  
    6.     private float spawnLimitXLeft = -22;
    7.     private float spawnLimitXRight = 7;
    8.     private float spawnPosY = 30;
    9.  
    10.     private float startDelay = 1.0f;
    11.     private float spawnInterval;
    12.  
    13.     // Start is called before the first frame update
    14.     void Start()
    15.     {
    16.         spawnInterval = Random.Range(2, 6);
    17.         InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
    18.     }
    19.  
    20.     // Spawn random ball at random x position at top of play area
    21.     void SpawnRandomBall ()
    22.     {
    23.         int ballIndex = Random.Range(0, ballPrefabs.Length);
    24.  
    25.         // Generate random ball index and random spawn position
    26.         Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
    27.  
    28.         // instantiate ball at random spawn location
    29.         Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
    30.     }
    31.  
    32. }
    33.  
    But it spawns balls around every 4 seconds at same rate all time.
    I have put the spawnInterval = Random.Range(2, 6); line in the spawnRandomBall method as it says to in the hints, but it doesn't keep spawning balls.

    Also how can I do the last bonus task of only allowing the player to spawn more dogs after certain amount of time has passed?
     
    mel619 likes this.
  2. sahilsharma220202004

    sahilsharma220202004

    Joined:
    Nov 18, 2020
    Posts:
    33
    copy the < spawnInterval = Random.Range(2, 6);> and paste it in a void update.
    so that its value can keep changing
    But keep it in start also as start is called before update.
     
  3. LONGQIUYU

    LONGQIUYU

    Joined:
    Jul 19, 2022
    Posts:
    5
    Hello, have you solved the problem? I have the same problem
     
  4. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please share your code with Code Tags and paste the error that you are receiving.
     
  5. LONGQIUYU

    LONGQIUYU

    Joined:
    Jul 19, 2022
    Posts:
    5
    Thank you for your reply. My code is in the following picture. The task requires a random time between 3 and 5 seconds to generate a ball. This is the method I tried, but it seems incorrect. I'm a beginner. I hope you can teach me how to solve this problem. Thank you!!!
     

    Attached Files:

  6. JeffDUnity3D

    JeffDUnity3D

    Joined:
    May 2, 2017
    Posts:
    14,446
    Please post with code tags as suggested, just like the first post in this thread, and not screenshots. But at any rate, is this code working? Why does it seem incorrect?
     
  7. LONGQIUYU

    LONGQIUYU

    Joined:
    Jul 19, 2022
    Posts:
    5
    After the code is run, the generation time of each ball is not random. It can only randomly select a number in 3-5 seconds, and then generate a ball at that time interval each time. This does not meet the requirements of the task.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnManagerX : MonoBehaviour
    6. {
    7.     public GameObject[] ballPrefabs;
    8.  
    9.     private float spawnLimitXLeft = -22;
    10.     private float spawnLimitXRight = 7;
    11.     private float spawnPosY = 30;
    12.  
    13.     private float startDelay = 1.0f;
    14.     private float spawnInterval;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         float spawnInterval = Random.Range(3, 5);
    20.         InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
    21.     }
    22.  
    23.     // Spawn random ball at random x position at top of play area
    24.     void SpawnRandomBall ()
    25.     {
    26.         //Randomly generated ball color
    27.         int ballIndex = Random.Range(0, ballPrefabs.Length);
    28.         spawnInterval = Random.Range(3, 5);
    29.  
    30.         // Generate random ball index and random spawn position
    31.         Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
    32.  
    33.         // instantiate ball at random spawn location
    34.         Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
    35.     }
    36. }
     
  8. LONGQIUYU

    LONGQIUYU

    Joined:
    Jul 19, 2022
    Posts:
    5
    After the code is run, the generation time of each ball is not random. It can only randomly select a number in 3-5 seconds, and then generate a ball at that time interval each time. This does not meet the requirements of the task.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnManagerX : MonoBehaviour
    6. {
    7.     public GameObject[] ballPrefabs;
    8.  
    9.     private float spawnLimitXLeft = -22;
    10.     private float spawnLimitXRight = 7;
    11.     private float spawnPosY = 30;
    12.  
    13.     private float startDelay = 1.0f;
    14.     private float spawnInterval;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         float spawnInterval = Random.Range(3, 5);
    20.         InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
    21.     }
    22.  
    23.     // Spawn random ball at random x position at top of play area
    24.     void SpawnRandomBall ()
    25.     {
    26.         //Randomly generated ball color
    27.         int ballIndex = Random.Range(0, ballPrefabs.Length);
    28.         spawnInterval = Random.Range(3, 5);
    29.  
    30.         // Generate random ball index and random spawn position
    31.         Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
    32.  
    33.         // instantiate ball at random spawn location
    34.         Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
    35.     }
    36. }
     
  9. SpiderFlash95

    SpiderFlash95

    Joined:
    Oct 31, 2022
    Posts:
    2
    Ask yourself following questions:
    1. When do I want a new random number? --> when the SpawnRandomBall function is called.
    2. When do I want to execute the SpawnRandomBall again? --> When I have a new random number generated.
    3. Does my Start function get updated? --> No, it only triggers the start of the game and calls the functions I type in there.

    I fixed it like this:
    Get rid of the repeat invoke in the start function and make it just an invoke to trigger the function to spawn a ball.
    Invoke("SpawnRandomBall", startDelay).

    Then in the SpawnRandomBall you want to set a random number between 3 and 5 every time this function is called.
    spawnInterval = Random.Range(fastSpawn, lateSpawn);
    Once you know the next interval you just call the same function again, but with the random interval number.
    Invoke("SpawnRandomBall", spawnInterval);

    Every time the SpawnRandomBall is called it will generate a new number and call the function again after that many seconds. Not sure if this is THE solution, but it works very neat.
     
    PezInSpace and Matt321123 like this.
  10. Matt321123

    Matt321123

    Joined:
    Aug 19, 2023
    Posts:
    1
    Great, thank you!
     
  11. PezInSpace

    PezInSpace

    Joined:
    Feb 12, 2024
    Posts:
    2
    ENGLISH (still learning it):
    SpiderFlash95 is right, "InvokeRepeating()" method set the interval as "hardcode" (so you can't change it even if you're using "Random()" method/func). As he/she said, you can use "Invoke()" in the "Start()" method and then use "Invoke()" again in "SpawnRandomBall()" method.

    ESPAÑOL:
    SpiderFlash95 tiene razón, el método "InvokeRepeating()" establece el intervalo como "código duro" (así que no lo puede cambiar incluso si utilizas el método/función "Random()"). Como él/ella dijo, puedes usar "Invoke()" en el método "Start()" y luego usarlo de nuevo en el método "SpawnRandomBall()".

    Example/Ejemplo:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class SpawnManagerX : MonoBehaviour
    6. {
    7.     public GameObject[] ballPrefabs;
    8.  
    9.     private float spawnLimitXLeft = -22;
    10.     private float spawnLimitXRight = 7;
    11.     private float spawnPosY = 30;
    12.  
    13.     private float startDelay = 1.0f;
    14.     private float spawnInterval;
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         //InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
    20.         Invoke("SpawnRandomBall", startDelay);
    21.     }
    22.  
    23.     // Spawn random ball at random x position at top of play area
    24.     void SpawnRandomBall ()
    25.     {
    26.         // Random spawnInterval
    27.         spawnInterval = Random.Range(3.0f, 5.0f);
    28.         Invoke("SpawnRandomBall", spawnInterval);
    29.         Debug.Log("Intevarlo: " + spawnInterval);
    30.  
    31.         // Generate random ball index and random spawn position
    32.         Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
    33.  
    34.         // instantiate ball at random spawn location
    35.         int ballIndex = Random.Range(0, ballPrefabs.Length);
    36.         Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
    37.  
    38.     }
    39. }
    40.