How to spawn at random times

on create with code challenge 2 how can I code the 2 bonus bits. Make the spawn interval a random value between 3 seconds and 5 seconds and Only allow the player to spawn a new dog after a certain amount of time has passed

This is my script.

public class SpawnManagerX : MonoBehaviour
{
public GameObject[ ] ballPrefabs;

private float spawnLimitXLeft = -22;
private float spawnLimitXRight = 7;
private float spawnPosY = 30;

private float startDelay = 1.0f;
private float spawnInterval = 4;
public float ballIndex;

// Start is called before the first frame update
void Start()
{
InvokeRepeating(“SpawnRandomBall”, startDelay, spawnInterval);
}

// Spawn random ball at random x position at top of play area
void SpawnRandomBall ()
{
// Generate random ball index and random spawn position
int ballIndex = Random.Range(0, ballPrefabs.Length);
Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);

// instantiate ball at random spawn location
Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);
}

}

Thanks for any help. Still learning

1 Like

You generate a random timer with the same Random.Range you’re already using.

float randomTimer = Random.Range(3f, 5f);  //random float between 3 and 5

Then you can just count it down by subtracting Time.deltaTime from it each update. When it is equal or less than 0f then you act.

If you need to make sure a certain amount of time has passed, after setting randomTimer to the random value, you can check if it is >= that certain amount of time which needs to have passed. If false, then set randomTimer to whatever that certain amount of time which needs to have passed is.

Good luck

2 Likes

Hello. While performing this task, I encountered a similar problem. The task hint states -" Hint - Set the spawnInterval value to a new random number between 3 and 5 seconds in the SpawnRandomBall method"

But when I start the game in the editor itself, only 1 ball falls.
I want to understand why this is happening. Thank you in advance for any help.

After various options, the solution turned out to be on the surface))

5 Likes

as alternative:

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

public class SpawnManagerX : MonoBehaviour
{
    public GameObject[] ballPrefabs;

    private float spawnLimitXLeft = -22;
    private float spawnLimitXRight = 7;
    private float spawnPosY = 30;

    private float startDelay = 2.0f;
    //private float spawnInterval;
    private float targetTime;

    // Start is called before the first frame update
    void Start()
    {
        //InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
        targetTime = 2;
    }
    private void Update()
    {
        targetTime -= Time.deltaTime;
        if (targetTime <= 0)
        {
            SpawnRandomBall();
            targetTime = Random.Range(3, 6);
            //Debug.Log(targetTime);
        }
        Debug.Log(targetTime);

    }

    // Spawn random ball at random x position at top of play area
    void SpawnRandomBall()
    {

        int ballIndex = Random.Range(0, ballPrefabs.Length);

        // Generate random ball index and random spawn position
        Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);

        // instantiate ball at random spawn location
        Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[0].transform.rotation);


        //Debug.Log(targetTime);
        //targetTime = 0;

    }

}
2 Likes

Hello, can you show me the correct and complete code? thank you very much!!

Please, share the code, I can’t solve it by myself :c

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.

8 Likes

Works Good, I had forgotten the concept of recursion. thanks

With respect mate, you’ve been around these parts just over 2 years, you should know to use CODE tags.

Hello everybody! That’s how I solved it in my code, maybe it will be useful to someone =)

public class SpawnManagerX : MonoBehaviour
{
public GameObject[ ] ballPrefabs;

private float spawnLimitXLeft = -22;
private float spawnLimitXRight = 7;
private float spawnPosY = 30;

private float startDelay = 1;
private float spawnInterval;

void Start()
{
Invoke("SpawnRandomBall", startDelay);
}

void SpawnRandomBall()
{
Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);
int randomBall = Random.Range(0, 3);

Instantiate(ballPrefabs[randomBall], spawnPos, ballPrefabs[randomBall].transform.rotation);

spawnInterval = Random.Range(3, 5);
Invoke("SpawnRandomBall", spawnInterval);
}

}
6 Likes

Hi all,
But…
Isn’t i a problem to recurse again and again…
For a game which dure for long and for witch the time repeating is short…
Will it not overlaod a recursive heap?

Thank you kindly.

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

public class SpawnManagerX : MonoBehaviour
{
public GameObject[ ] ballPrefabs;

private float spawnLimitXLeft = -22;
private float spawnLimitXRight = 7;
private float spawnPosY = 30;

private float startDelay = 1.0f;

// Start is called before the first frame update
void Start()
{
int spawnInter = Random.Range(1, 25);
InvokeRepeating(“SpawnRandomBall”, startDelay, spawnInter);
}

// Spawn random ball at random x position at top of play area
void SpawnRandomBall ()
{
// Generate random ball index and random spawn position
Vector3 spawnPos = new Vector3(Random.Range(spawnLimitXLeft, spawnLimitXRight), spawnPosY, 0);

// instantiate ball at random spawn location
int ballIndex = Random.Range(0, ballPrefabs.Length);
Instantiate(ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation);

}

}

1 Like

Thanks this help didnt realise that i shouldve but it in the start method