0
\$\begingroup\$
 void Update()
{
    if (Time.time > nextSpawnTime)
    {
        FirstSpawnScript();
        //SecondSpawnScript();
        //ThirdSpawnScript();
        //FourthSpawnScript();
    }

}
void FirstSpawnScript()
{
    float SecondsBetweenSpawns = Mathf.Lerp(SecondsBetweenSpawnsMinMax.y, SecondsBetweenSpawnsMinMax.x, Difficulty.GetDifficultyPercent());
    {
            print(SecondsBetweenSpawns);
                float SpawnAngle = Random.Range(-SpawnAngleMax, SpawnAngleMax);
                nextSpawnTime = Time.time + SecondsBetweenSpawns;
                Vector2 spawnPosition = new Vector2(Random.Range(-ScreenWrapSpawn.x, ScreenWrapSpawn.x), ScreenWrapSpawn.y + .5f);
                Instantiate(FirstSpawn, spawnPosition, Quaternion.Euler(Vector3.forward * SpawnAngle));
            Spawning = false;

         
        
        
    }
}

void SecondSpawnScript()
{
    float SecondsBetweenSpawns = Mathf.Lerp(SecondsBetweenSpawnsMinMax.y, SecondsBetweenSpawnsMinMax.x, Difficulty.GetDifficultyPercent());
    {
        {
            nextSpawnTime = Time.time + SecondsBetweenSpawns;
            float SpawnAngle = Random.Range(-SpawnAngleMax, SpawnAngleMax);
            Vector2 spawnPos = new Vector2(Random.Range(-ScreenWrapSpawn.x, ScreenWrapSpawn.x), ScreenWrapSpawn.y + .5f);
            Instantiate(SecondSpawn, spawnPos, Quaternion.Euler(Vector3.forward * SpawnAngle));
            
          

        }
      
    }

These are the two function i mean

\$\endgroup\$
1
  • 1
    \$\begingroup\$ If you have later 100 spawns, you do not want to write 100 of these functions. Rather write a single one that takes your XSpawn as parameter. \$\endgroup\$
    – Zibelas
    Jan 21 at 8:05

1 Answer 1

1
\$\begingroup\$

I think you are looking for coroutines.

A coroutine is a method which can interrupt itself using the yield return statement in order to be later resumed on another frame. This is useful when you have sequences of actions you want to perform within pauses between them instead of all at once. This example will log "1", "2", and "3" to the console with 1 second pauses in between:

class CoroutineExample : MonoBehaviour {
    
    // start a coroutine when this object is created
    void Start() {
        StartCoroutine(Count());
    }
 
    // the coroutine
    private IEnumerable Count() {
        Debug.Log("1");
        yield return new WaitForSeconds(1f);
        Debug.Log("2");
        yield return new WaitForSeconds(1f);
        Debug.Log("3");
    }
}

the yield return statement can use more than just new WaitForSeconds. For example, you can pass another coroutine method. This allows you to chain coroutines. This example will first execute the first coroutine (logging "1", "2" and "3"). It will wait until the coroutine is finished, then wait for another second, and then start the second coroutine (logging "A", "B", "C");

class CoroutineExample : MonoBehaviour {
    
    // start a coroutine when this object is created
    void Start() {
        StartCoroutine(CountAndSpell());
    }

    // The main coroutine chaining two other coroutines
    private IEnumerable CountAndSpell() {
         yield return Count();
         yield return new WaitForSeconds(1f);
         yield return Spell();
    }
         
    // Sub-Coroutine 1
    private IEnumerable Count() {
        Debug.Log("1");
        yield return new WaitForSeconds(1f);
        Debug.Log("2");
        yield return new WaitForSeconds(1f);
        Debug.Log("3");
    }

    // Sub-Coroutine 2
    private IEnumerable Spell() {
        Debug.Log("A");
        yield return new WaitForSeconds(1f);
        Debug.Log("B");
        yield return new WaitForSeconds(1f);
        Debug.Log("C");
    }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .