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

RayCastHit2d returns NullReferenceException

Discussion in '2D' started by HiKaylum, May 11, 2014.

  1. HiKaylum

    HiKaylum

    Joined:
    May 11, 2014
    Posts:
    4
    Hey guys,

    So i'm getting this error every time I click on a blue ball with the tag "blueBall" and I cant seem to find how to fix it, the code works and only sets the object the ray hit with that tag as detective but the exception keeps popping up.

    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4.  
    5. public class BallScript : MonoBehaviour {
    6.  
    7.     Ray ray;
    8.     RaycastHit2D rayHit;
    9.  
    10.     void Update () {
    11.         ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    12.         rayHit = Physics2D.Raycast(ray.origin, ray.direction);
    13.         if(Input.GetKeyDown(KeyCode.Mouse0)){
    14.             if(rayHit.collider.tag == "blueBall"){
    15.                 rayHit.collider.gameObject.SetActive(false);
    16.                 GameManager.gameManager.SetPlayerScore(GameManager.gameManager.GetPlayerScore()+1);
    17.             }else{
    18.                 Debug.Log("Nada");
    19.             }
    20.         }
    21.     }
    22. }
    23.  
     
  2. Zilk

    Zilk

    Joined:
    May 10, 2014
    Posts:
    322
    Do you deactivate the blueBall gameObject somewhere else before?

    Also you could probabaly use rayHit.gameobject.SetActiva(false) and skip collider.
     
  3. hippocoder

    hippocoder

    Digital Ape Moderator

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    rayHit will always return true, so add a conditional check like if (rayHit.collider != null) ... because if the ray misses you'll have bogus null data otherwise and it will crash.


    You're blindly expecting to check data that doesn't exist:

    rayHit.collider.tag

    Without a conditional first, to check that rayHit.collider exists, it will simply be accessing data that does not exist, causing your crash.
     
  4. HiKaylum

    HiKaylum

    Joined:
    May 11, 2014
    Posts:
    4
    Ya at the start of the game all balls used are instatiated and put into an array and deactivated, then I have a script that sets random balls a position and sets them as active.

    I have to use rayHit.collider because rayHit doesn't have a definition for gameObject.
     
  5. HiKaylum

    HiKaylum

    Joined:
    May 11, 2014
    Posts:
    4
    ya I know I stuck that back in, I took it out before posting thinking that may have been the error.

    [UPDATE]

    I read over what you wrote and what I picked up from what you said instead of checking if rayHit.collider exists in the same if statement as checking for the tag I mad another if statement before the check for the tag and it worked so thanks man :)
     
    Last edited: May 11, 2014