Home > Back-end >  c# Spawn Interval Changer
c# Spawn Interval Changer

Time:11-25

I add my codes below. What is my fault, can anyone help me? I want to when SpawnRandomBall function run two times, spawnInternal turn into spawnInternal2. So I create a new variable, called 'check'. The variable increase when SpawnRandomBall function run. I set the variable as a public. In this way I can see that 'check' variable increase or doesn't increase. 'Check' variable is increasing without problem. When the veriable value equal to 3, it must be 'else if' run. But unfortunately it doesn't work.

I guess problem is I run my codes in Start() function. But I don't know how can I do properly.

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

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

    private float spawnLimitXLeft = 14.5f;
    private float spawnLimitXRight = 24;
    private float spawnPosY = 10;

    private float startDelay = 1.0f;
    private float spawnInterval = 4.0f;
    private float spawnInterval2 = 2.0f;
    public int check;

    // Start is called before the first frame update
    void Start()
    {
        if (check <= 2)
        {
            InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval);
        }
        else if (check > 2)
        {
            InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval2);
        }
    }
    // 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(-21, spawnPosY, Random.Range(spawnLimitXLeft, spawnLimitXRight));
        int ballIndex = Random.Range(0, 3);

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

}

I want to change SpawnInternal variable into SpawnInternal2

CodePudding user response:

The situation you describe is that you want a method to run. And for the first two iterations, you’d like one particular delay interval, and then after that you’d like a different delay interval. With that being the case, I would suggest you’re using the wrong tools. I would personally start a Coroutine like this:

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

    private float spawnLimitXLeft = 14.5f;
    private float spawnLimitXRight = 24;
    private float spawnPosY = 10;

    private float startDelay = 1.0f;
    private float spawnInterval = 4.0f;
    private float spawnInterval2 = 2.0f;
    public int check;

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

    // Spawn random ball at random x position at top of play area
    IEnumerator SpawnRandomBall ()
    {
        while ( true )
        {
            // Generate random ball index and random spawn position
            var spawnPos = new Vector3(-21, spawnPosY, Random.Range(spawnLimitXLeft, spawnLimitXRight));
            var ballIndex = Random.Range(0, 3);

            // instantiate ball at random spawn location
            Instantiate( ballPrefabs[ballIndex], spawnPos, ballPrefabs[ballIndex].transform.rotation );
    
        yield return new WaitForSeconds ( 
            (check   < 2) ? spawnInterval : spawnInterval2 );
        }
    }
}

It’s also possible to run the Start method as a coroutine as well, but I feel, especially for newcomers, that the intent can sometimes be lost.

What this is saying is, start a coroutine, and in that coroutine, keep looping while (true) (which means “forever”). The line that change the amount of time that it waits is the last line, the “return”. We WaitForSeconds and the number of seconds we wait is based on the current count value. NB: written on phone but not tested.

CodePudding user response:

If Start gets called once then the else if is never run. The code doesn't just wait at the else if waiting for check to update.

You just need to manage what code gets run in the SpawnRandomBall method.

You're going to need to try something like this this:

// Start is called before the first frame update
void Start()
{
    InvokeRepeating("SpawnRandomBall", startDelay, spawnInterval2);
}
// Spawn random ball at random x position at top of play area
void SpawnRandomBall()
{
    check  ;
    if (check == 1 || check == 3)
        return;
        
    // Generate random ball index and random spawn position
    Vector3 spawnPos = new Vector3(-21, spawnPosY, Random.Range(spawnLimitXLeft, spawnLimitXRight));
    int ballIndex = Random.Range(0, 3);

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