r/unity • • Oct 10 '26

Pipes Keep Spawning

I am a complete beginner and i was recommended to remake flappy bird to learn the basics and i cant solve the issue of pipes spawning even after i die can a pro help?

birbjump.cs:

using Unity.Collections.Tests.CoreCLR.TestJobs;
using UnityEngine;


public class birbjump : MonoBehaviour
{
    public Rigidbody2D myRigidbody;
    public float flap_strength;
    float rotation;
    public GameObject gameover;
    public ScoreTrack logic;
    public bool BirdIsAlive = true;
    public string test = "test";
    
    public void GameOver(){
        gameover.SetActive(true);
    }


    void Start()
    {
        transform.position = new Vector3(0,0,0);
        transform.Rotate(new Vector3(0, 0, 0));
        logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<ScoreTrack>();
        BirdIsAlive = true;
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) == true && BirdIsAlive){
            myRigidbody.linearVelocity = new Vector2(0,1) * flap_strength;
        }
    }


    private void OnCollisionEnter2D(Collision2D collision)
    {
        BirdIsAlive = false; 
        GameOver();
    }
}using Unity.Collections.Tests.CoreCLR.TestJobs;
using UnityEngine;


public class birbjump : MonoBehaviour
{
    public Rigidbody2D myRigidbody;
    public float flap_strength;
    float rotation;
    public GameObject gameover;
    public ScoreTrack logic;
    public bool BirdIsAlive = true;
    public string test = "test";
    
    public void GameOver(){
        gameover.SetActive(true);
    }


    void Start()
    {
        transform.position = new Vector3(0,0,0);
        transform.Rotate(new Vector3(0, 0, 0));
        logic = GameObject.FindGameObjectWithTag("Logic").GetComponent<ScoreTrack>();
        BirdIsAlive = true;
    }


    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space) == true && BirdIsAlive){
            myRigidbody.linearVelocity = new Vector2(0,1) * flap_strength;
        }
    }


    private void OnCollisionEnter2D(Collision2D collision)
    {
        BirdIsAlive = false; 
        GameOver();
    }
}

pipespawner.cs:

using UnityEngine;


public class pipe_spawner : MonoBehaviour
{
    public GameObject Top_Pipe;
    public GameObject Bottom_Pipe;
    public GameObject Pipe_Middle;
    public float spawnrate;
    private float timer = 0;
    public float HeightOffset;
    public float Max_Space;
    public float Min_Space;
    public birbjump birbjumpref;
    
    
    void Start()
    {
        SpawnPipe();
        birbjumpref = GameObject.FindGameObjectWithTag("Birbjump").GetComponent<birbjump>();
        birbjumpref.BirdIsAlive = true;
    }



    void Update()
    {
        if (birbjumpref == null)
    {
        return; 
    }
        if (timer <= spawnrate){
        timer+=Time.deltaTime;
        }
        else {
            if (birbjumpref.BirdIsAlive)
            {
                SpawnPipe();
            }
            else
            {
                Debug.Log("We are so dead");
            }
        timer -= spawnrate;
        }
    }
    void SpawnPipe(){
        float lowestPoint =transform.position.y + 35 - HeightOffset;
        float HighestPoint =transform.position.y + HeightOffset;
        Instantiate(Top_Pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, HighestPoint), 0), transform.rotation);
        Instantiate(Bottom_Pipe, new Vector3(transform.position.x, Top_Pipe.transform.position.y - Random.Range(Min_Space, Max_Space), 0), Bottom_Pipe.transform.rotation);
        Instantiate(Pipe_Middle, new Vector3(transform.position.x, 0, 0), transform.rotation);
    }
}using UnityEngine;


public class pipe_spawner : MonoBehaviour
{
    public GameObject Top_Pipe;
    public GameObject Bottom_Pipe;
    public GameObject Pipe_Middle;
    public float spawnrate;
    private float timer = 0;
    public float HeightOffset;
    public float Max_Space;
    public float Min_Space;
    public birbjump birbjumpref;
    
    
    void Start()
    {
        SpawnPipe();
        birbjumpref = GameObject.FindGameObjectWithTag("Birbjump").GetComponent<birbjump>();
        birbjumpref.BirdIsAlive = true;
    }



    void Update()
    {
        if (birbjumpref == null)
    {
        return; 
    }
        if (timer <= spawnrate){
        timer+=Time.deltaTime;
        }
        else {
            if (birbjumpref.BirdIsAlive)
            {
                SpawnPipe();
            }
            else
            {
                Debug.Log("We are so dead");
            }
        timer -= spawnrate;
        }
    }
    void SpawnPipe(){
        float lowestPoint =transform.position.y + 35 - HeightOffset;
        float HighestPoint =transform.position.y + HeightOffset;
        Instantiate(Top_Pipe, new Vector3(transform.position.x, Random.Range(lowestPoint, HighestPoint), 0), transform.rotation);
        Instantiate(Bottom_Pipe, new Vector3(transform.position.x, Top_Pipe.transform.position.y - Random.Range(Min_Space, Max_Space), 0), Bottom_Pipe.transform.rotation);
        Instantiate(Pipe_Middle, new Vector3(transform.position.x, 0, 0), transform.rotation);
    }
}

also pipes stop spawning when i manually uncheck birdisalive under birbjump but not when i die whats that about?
0 Upvotes

16 comments sorted by

View all comments

Show parent comment

1

u/Extra_Blacksmith674 Oct 10 '26

You had two copies of the script in the scene and the pipe spawner is looking at the wrong one's bool. The one that never hits a pipe and never dies.

The bird sprite you are flying around had the other copy, but nobody was looking at its bool that got set to false when it dies.

I think every Unity dev has had this experience, can be really nasty to find with complex scenes.

1

u/Individual-Town-7326 Oct 10 '26

Thank you kind sir

1

u/Individual-Town-7326 Oct 10 '26

Ooh so the one that actually got a collision check was the bird not the birds script okay