-2

Write, so, after learning some basic C# i've gone on to follow a tutorial to get the VERY basics down of Unity.

I'm trying to use the boolean birdIsAlive from BirdScript.cs:

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

public class BirdScript : MonoBehaviour
{
    public Rigidbody2D myRigidBody;
    public float flapStrength;
    public LogicScript logic;
    public bool birdIsAlive;

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

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

        }

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

    }

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

}

I'm trying to use it here (line 25 is where the issue is)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using static UnityEngine.GraphicsBuffer;

public class PipeMiddleScript : MonoBehaviour
{

    public LogicScript logic;
    public BirdScript bird;

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

        }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.layer == 3 && bird.birdIsAlive == true)
        {
            logic.addScore(1);
        }
    }
}

Any help would be much appreciated!

I thought that referencing the BirdScript at the top would allow me to use the boolean to check if the bird is alive before adding score for crossing a trigger.

itsEg0
  • 1
  • 1
  • 2
    this answers your question: [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ňɏssa Pøngjǣrdenlarp May 15 '23 at 01:36
  • Where is `bird` ever assigned a value in `PipeMiddleScript`? I assume it isn't assigned in the inspector because you'd probably do the same for `logic` and you have a lookup at runtime for that. `bird` likely needs the same thing. – Retired Ninja May 15 '23 at 01:38
  • @RetiredNinja I'm sorry, i'm new lol. I thought I could use what I used (public LogicScript logic) to use the functions within that script and apply that to get booleans from BirdScript into PipeMiddleScript. – itsEg0 May 15 '23 at 01:43

1 Answers1

0

you are using the property of bird script bird.birdIsAlive without adding this in your start() method bird = FindObjectOfType<Bird>();