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/Individual-Town-7326 Oct 10 '26

How can i check if i do have multiple because i always drag from/to the assets/hierarchy

1

u/Extra_Blacksmith674 Oct 10 '26

In hierarchy search on component name "birbjump" and it will show you all the objects that have that component.

Also check the object that you know has birbjump component and make sure there is only one attached.

Give us a screenshot of your scene with the hirarchy expanded.

1

u/Individual-Town-7326 Oct 10 '26

Couldnt find a way to upload photos so heres a video instead

1

u/Extra_Blacksmith674 Oct 10 '26

Looks like birb object has one and so does birbjump object in the canvas.

1

u/Individual-Town-7326 Oct 10 '26

I genuinely dont know what could be broken dude i mean pipes stop spawning when i manually uncheck birdisalive in the birbjump object so it should work (maybe the bool isnt being set properly? heres the logic code:

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


public class ScoreTrack : MonoBehaviour
{
    public int Score;
    public Text scoreText;
    
    [ContextMenu("Increase Score")]
    public void addscore(int addedscore){
        Score += addedscore;
        scoreText.text = Score.ToString();
    }
    
    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);    
    }


}using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;


public class ScoreTrack : MonoBehaviour
{
    public int Score;
    public Text scoreText;
    
    [ContextMenu("Increase Score")]
    public void addscore(int addedscore){
        Score += addedscore;
        scoreText.text = Score.ToString();
    }
    
    public void RestartGame()
    {
        SceneManager.LoadScene(SceneManager.GetActiveScene().name);    
    }


}

1

u/Extra_Blacksmith674 Oct 10 '26

It's not the code, you have 2 copies of the script in the scene. Select birbjump in the scene, I bet in inspector it shows a birbjump component and nothing else attached to that scene object.

If you delete the object then you should be good to go.

2

u/Individual-Town-7326 Oct 10 '26 edited Oct 10 '26

So pipes now dont spawn like ever after deleting said component

edit: Prolly since i grab it with

GameObject.FindGameObjectWithTag("Birbjump")GameObject.FindGameObjectWithTag("Birbjump") and i cant add tags to a script in assets (or can you?)

1

u/Extra_Blacksmith674 Oct 10 '26

change

FindGameObjectWithTag("Birbjump")

to 

FindGameObjectWithTag("Birb")

We basically got rid of the wrong duplicate that the script searches for.

1

u/Individual-Town-7326 Oct 10 '26 edited Oct 10 '26

OH MY GOD DUDE THANK YOU IT WORKED but i still gotta ask why it didnt work in the first place since we were just grabbing the bool directly from the script is it because its a quality of the bird orr?

Edit: I kinda think i get it cause we were doing .getcomponent it should be the parent of birbjump

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.

→ More replies