Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question The name 'birdIsAlive' does not exist in the current context

Discussion in 'Scripting' started by Avocado_olaf_, Aug 13, 2023.

  1. Avocado_olaf_

    Avocado_olaf_

    Joined:
    Nov 12, 2022
    Posts:
    2
    Hello I am new to unity and I am trying to make a flappy bird style game but I can not find how to creat a reference inbetween two scripts and I get this error code "The name 'birdIsAlive' does not exist in the current context" here are the scripts

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

    public class birdScript : MonoBehaviour
    {
    public Rigidbody2D myRigidbody;
    public float flapStrenght;
    private LogicScripts logic;
    public bool birdIsAlive = true;






    // Start is called before the first frame update
    void Start()
    {
    logic = GameObject.FindGameObjectWithTag("logic").GetComponent<LogicScripts>();
    }

    // Update is called once per frame
    void Update()
    {
    if (Input.GetKeyDown(KeyCode.Space) == true && birdIsAlive == true)
    {
    myRigidbody.velocity = Vector2.up * flapStrenght;
    }

    if(transform.position.y > 19)
    {
    logic.gameOver();
    birdIsAlive = false;
    }
    if(transform.position.y < -19)
    {
    logic.gameOver();
    birdIsAlive = false;
    }

    }
    private void OnCollisionEnter2D(Collision2D collision)
    {
    logic.gameOver();
    birdIsAlive = false;
    }
    }




    and here is the second one




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

    public class PipeMiddleScript : MonoBehaviour
    {

    public LogicScripts logic;
    public birdScript bird;


    // Start is called before the first frame update
    void Start()
    {
    logic = GameObject.FindGameObjectWithTag("logic").GetComponent<LogicScripts>();
    bird = GameObject.Find("bird").GetComponent<birdScript>();
    }



    // Update is called once per frame
    void Update()
    {

    }
    private void OnTriggerEnter2D(Collider2D collision)
    {
    if (birdIsAlive = true)
    {
    logic.AddScore(1);

    }






    }
    }





    I am trying to fix a bug where If you pass in between the posts you get a point even if your dead
    Thank you for your help
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    u need to do bird.birdIsAlive
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,468
    Please edit your post to use code-tags. Don't use plain text to post code.

    Thanks,