0

In my Unity 2D game, I have set up 3 different 2D Raycasts on the same GameObject for score-detection purposes with the help of boolean and if statements. The object in question today is a torus that is scrolling from right to left, which the player (the balloon) has to go through or avoid using up and down motion. The player gets more points when going through the torus as opposed to going around it.

The issue: Rayhit1 returns correctly when the player is detected inside the torus. But the code for Rayhit2 and Rayhit3 won't execute. Interestingly, if I put the code for Rayhit2 before Rayhit1, it would execute. Same is true for Rayhit3.

This is the "hit1" part for example:

if(!Rayhit1)
    {
         if(hit1.collider.name == "GameObject")
         {
              RayHit1 = true;
              score.value += 30;
         }
    }
if(!hit1)
    {
         RayHit1 = false;
    }

Snapshot of the game

Here are snippets of my code that concern the process of detecting score:

private bool RayHit1 = false;
private bool RayHit2 = false;
private bool RayHit3 = false;

void Update()
{
     int layer = 9;
     int layerMask = 1;
     RaycastHit2D hit1 = Physics2D.Raycast(new Vector2(transform.position.x + 5.0f, transform.position.y - 72.0f), Vector2.up, 150.00f, layerMask);
     RaycastHit2D hit2 = Physics2D.Raycast(new Vector2(transform.position.x + 5.0f, transform.position.y + 85.0f), Vector2.up, 600.00f, layerMask);
     RaycastHit2D hit3 = Physics2D.Raycast(new Vector2(transform.position.x + 5.0f, transform.position.y - 85.0f), Vector2.down, 600.00f, layerMask);

if(!Rayhit1)
{
     if(hit1.collider.name == "GameObject")
     {
          RayHit1 = true; //boolean to true for just 1 frame so that the score does not add more than once
          score.value += 30;
     }
}
if(!hit1)
{
     RayHit1 = false; //reset boolean to false when Raycast returns nothing
}

if(!Rayhit2)
{
     if(hit2.collider.name == "GameObject")
     {
          RayHit2 = true;
          score.value += 10;
     }
}
if(!hit2)
{
     RayHit2 = false;
}

if(!Rayhit3)
{
     if(hit3.collider.name == "GameObject")
     {
          RayHit3 = true;
          score.value += 10;
     }
}
if(!hit3)
{
     RayHit3 = false;
}
 
}
  • What does "does not return anything" mean? Is the result `null`? What are the arguments `a`, `b`, `c`, and `d`? – Ruzihm Jul 29 '21 at 15:44
  • Argument "a" is the origin point, "b" is the direction, "c" is the length, "d" is the layerMask of the Raycast. No, the result is not null. My issue is that the remaining if statements are never checked after the first two. – Kyaw Zaw Aung Jul 29 '21 at 15:48
  • 1
    How can you claim the arguments arent the same but list abcd for all of them. Minimal viable code – BugFinder Jul 29 '21 at 15:51
  • I have inserted the original code that should have been in place of abcd. I don't know how to format code blocks here so I apologize truly for making lines so long. – Kyaw Zaw Aung Jul 29 '21 at 15:55
  • 1
    Next time please include that you hit a NullReferenceException. All exceptions and errors should be included in the question. Does this answer 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) – Ruzihm Jul 29 '21 at 16:35

1 Answers1

0

I think you may have NullReferenceException at the value hit1.collider in

if(hit1.collider.name == "GameObject")
     {
          RayHit1 = true; //boolean to true for just 1 frame so that the score does not count add than once
          score.value += 30;
     }

Try checking if the hit has collided before reading the name :

if(!Rayhit1)
{
     if(hit1 && hit1.collider.name == "GameObject")
     {
          RayHit1 = true; //boolean to true for just 1 frame so that the score does not count add than once
          score.value += 30;
     }
}
Ironax
  • 16
  • 1
  • Interesting! Your advice was spot on to fix the issue. Now all my if statements on the Raycasts detect the player correctly. I understand that NullReferenceException occurs when I don't explicitly reference an object, but what exactly was inconsistent with my original code? My guess is that I was checking for the name of the player without telling the script that the player was detected in the first place. Yeah, I think that was the issue. I appreciate your help sir. – Kyaw Zaw Aung Jul 29 '21 at 16:10