Search Unity

Resolved Raycasting null [FIXED] [NEW] Won't destroy prefab

Discussion in 'Scripting' started by nsteele1031, Jan 11, 2022.

  1. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    I get a null error when I run my code and I don't know what is null I thought it will get the info from the ray and make it not null when I will use it. Im new to unity so sorry if its a simple fix. And please don't just give a website to how to fix it and just explain whats wrong to my code.
    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     private float speed = 5;
    4.     private RaycastHit hitData;
    5.     private Ray ray = new Ray();
    6.     // Start is called before the first frame update
    7.     void Start()
    8.     {
    9.        
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update()
    14.     {
    15.         if (Input.GetKey(KeyCode.W))
    16.         {
    17.             transform.Translate(Vector3.forward * Time.deltaTime * speed);
    18.         }
    19.        
    20.         if (Input.GetKey(KeyCode.S))
    21.         {
    22.             transform.Translate(Vector3.forward * Time.deltaTime * -speed);
    23.         }
    24.  
    25.         if (Input.GetKey(KeyCode.A))
    26.         {
    27.             transform.Translate(Vector3.right * Time.deltaTime * -speed);
    28.         }
    29.  
    30.         if (Input.GetKey(KeyCode.D))
    31.         {
    32.             transform.Translate(Vector3.right * Time.deltaTime * speed);
    33.         }
    34.  
    35.         if (Input.GetMouseButtonDown(0))
    36.         {
    37.             Physics.Raycast(ray, out hitData);
    38.             if (hitData.collider.tag == "Tree")
    39.             {
    40.                 Destroy(hitData.collider.gameObject);
    41.             }
    42.         }
    43.     }
    44. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    The problem is likely that you're entering that code whether you hit anything or not. Raycast informs you whether it hit something via its return value, so generally you want to do something like this:
    Code (csharp):
    1. if (Physics.Raycast(...)) {
    2. //code that uses your hitData
    3. }
     
    GroZZleR likes this.
  3. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
  4. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    Alright so I am not getting the error anymore but it won't destroy the trees the ray casting touches. Is there some way i can refrance the thing the ray is colliding. Or is the way I'm doing it the right way
     
  5. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    You can put in something like Debug.Log($"We hit {hitData.collider.name}");
    Most likely, the raycast is hitting nothing, so that log won't help. You can use Debug.DrawRay(ray) outside the if block to draw a ray in the scene view to visualize where the ray is. This is almost certainly the problem, since you never set the value of 'ray' to anything, so I'd expect to see the ray being drawn straight forward from the scene's origin.
     
  6. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    i figured it out thank you for the help