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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Resolved Raycast2D issue

Discussion in 'Scripting' started by DJF12345, Nov 18, 2022.

  1. DJF12345

    DJF12345

    Joined:
    Oct 4, 2022
    Posts:
    6
    I'm having an issue with Raycast2D, I'm getting the error below. Not sure what the issue is. All required/referenced objects are in the Inspector. What am I getting wrong here?



    Code (CSharp):
    1. playerSpotted = Physics2D.Raycast(transform.position, lookToward.transform.position, lookDistance, LayerMask.GetMask("Player"));
    2.  
    3.         if(playerSpotted.collider.CompareTag("Player")) // This is line 35
    4.         {
    5.             Debug.Log("Player spotted");
    6.         }
    NullReferenceException: Object reference not set to an instance of an object
    EnemyController.Update () (at Assets/Scripts/EnemyController.cs:35)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    DJF12345 likes this.
  3. DJF12345

    DJF12345

    Joined:
    Oct 4, 2022
    Posts:
    6
    Thanks Kurt. I tend to go for that approach anyway, but perhaps I wasn't thorough enough. I honestly try to exhaust every option I have before asking on here as I don't want to waste people's time, plus I feel like I learn more when I solve the problem myself, and believe me there would be 1000 posts from me on here if I didn't do this.

    So, I looked at the four elements of the Raycast function individually and what I get back for LayerMask.GetMask("Player") is 64.

    My understanding is that there are 32 layers, and Player is layer 6 when I look at it. So if it is returning 64 then Unity doesn't know what to look for and returns a null? Could this be correct or am I misinterpreting things?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,946
    Unity absolutely does not know what it is looking for. YOU should know what you're looking for.

    Physics.Raycast() takes a ton of arguments so writing it all on one line can be problematic.

    Break the inputs apart and see what you're passing in. Use Debug.Log() extensively.

    Always use named arguments with
    Physics.Raycast()
    because it contains many poorly-designed overloads:

    https://forum.unity.com/threads/lay...ke-described-in-the-docs.744302/#post-6355392

    Beyond that, based on the docs you should realize that using the .collider property without testing it is going to be a problem when you don't hit anything with the raycast.

    In fact, you should wrap the raycast call in an if() statement, as is often done to simplify checking.
     
  5. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Despite the misleading title, this isn't a "Raycast2D" issue, it's your script having a reference to an object type that is NULL. It's nothing to do with layers, physics etc.

    Do you show "EnemyController.cs" line 35? You should indicate what specific line that is, we have no idea.

    If Line 35 is line 1 above then only "lookToward" can be NULL from the look of it. If it's line 3 then note that you're not checking if you hit something or not, you just assume you do. If you don't then the "collider" property will be NULL.

    You should always check the hit result before using it so in your case do "if (playerSpotted) { <do the stuff> }"
     
  6. DJF12345

    DJF12345

    Joined:
    Oct 4, 2022
    Posts:
    6
    There's a comment in the code I provided that does exactly this. Not to worry, I asked on Reddit and I received a very warm and friendly response that helped me.

    Cheers.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,620
    Ah I missed that, glad to see you focused on that and suggested the rest of the post wasn't the answer i.e. not checking if the hit result was valid.