0

In my flappy bird game, I want to create a loop that works through instantiated pipes, altering the pipe prefab (to reduce space between pipes + increase respawn rate) to increase difficulty based on player score.

The code below works, but I'd like to implement difficulty differently. I don't know how to add newly instantiated objects to a list.

I'd also like to add an event animation that plays after each level (before new pipe prefab instantiated), pausing the loop as the "new level" text flies across the screen.

    private void Start()
    {
        InvokeRepeating("SpawnPipe", .05f, respawnRate);
    }
  
private void SpawnPipe()
    {
        int levelOne = 8;
        int levelTwo = 18;
        int levelThree = 29;


        if(GameManager.score < levelOne)
        {
            GameObject pipes1 = Instantiate(pipePrefab1, transform.position, Quaternion.identity);
            pipes1.transform.position += Vector3.up * Random.Range(minHeight, maxHeight);
        }

        if (GameManager.score > levelOne && GameManager.score < levelTwo)
        {
            GameObject pipes2 = Instantiate(pipePrefab2, transform.position, Quaternion.identity);
            pipes2.transform.position += Vector3.up * Random.Range(minHeight, maxHeight);
        }

        if (GameManager.score > levelTwo && GameManager.score < levelThree)
        {
            GameObject pipes3 = Instantiate(pipePrefab3, transform.position, Quaternion.identity);
            pipes3.transform.position += Vector3.up * Random.Range(minHeight, maxHeight);
        }

    }

1 Answers1

0

Not sure if this is what you are asking for but Lists are used like this:

List<GameObject> pipes = new List<>();
pipes.add(pipes1);

Using InvokeRepeating to spawn the pipes is probably a bad idea because it gives you little control. I would recommend to check in your Update loop if a certain time passed or distance was traveled and spawn pipes accordingly;

David_RotU
  • 26
  • 2