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 Flappy if-statement

Discussion in '2D' started by TokoHagar, Jun 4, 2023.

  1. TokoHagar

    TokoHagar

    Joined:
    Apr 29, 2023
    Posts:
    9
    So I'm trying to make a flappy bird type game. I want my bird to only get points when it is alive since when it dies, sometimes it ragdolls a bit and it gets a point.

    So this was my add-point statement originally:
    Code (CSharp):
    1.  private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.             logic.addScore();
    4.     }
    So I wanted it to not get points anymore when it dies, so I changed it to this:
    Code (CSharp):
    1.  private void OnTriggerEnter2D(Collider2D collision)
    2.     {
    3.         if(neilscrip.birdIsAlive == true)
    4.             logic.addScore();
    5.     }
    It should have fixed the problem, but now my bird isn't getting any points. When I pass through a pipe, instead of getting a point, I get an error code:

    "NullReferenceException: Object reference not set to an instance of an object
    PipeMiddleScript.OnTriggerEnter2D (UnityEngine.Collider2D collision) (at Assets/PipeMiddleScript.cs:24)"

    My PipeMiddleScript.cs.24 is:
    Code (CSharp):
    1. if(neilscrip.birdIsAlive == true)
    What can I do to fix this?
     
  2. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    assign a reference to neilscrip.
     
    TokoHagar likes this.
  3. TokoHagar

    TokoHagar

    Joined:
    Apr 29, 2023
    Posts:
    9
    Thank you so much! I was confused since my pipe isn't actually spawned yet so I'd have to reference it using tags. Thanks!