-1

The following code produces the error:

if (hitInfo.transform.name == "jumppad" && isJumping == false)

The error is being given because the raycast is not hitting anything such as a collider, it is just going out into open space.

This is the error that is showing up:

The error that is showing up

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class gun_script : MonoBehaviour
{

    public Transform firepoint;

    public LineRenderer linerend;
    public Transform gun;
    public float mouseSens;
    public int jumpHeight = 5;
    float lookangle;
    public Rigidbody2D rbForPlayer;
    Vector2 lookDirection;
    private bool isJumping = false;

    // Update is called once per frame
    void Update()
    {
        isJumping = false;
        lookDirection = Camera.main.WorldToScreenPoint(Input.mousePosition);
        lookangle = Mathf.Atan2(lookDirection.y, lookDirection.x) * Mathf.Rad2Deg;

        gun.rotation = Quaternion.Euler(0, 0, lookangle * mouseSens);

        RaycastHit2D hitInfo = Physics2D.Raycast(firepoint.position, firepoint.right);

        if (hitInfo)
        {
            linerend.SetPosition(0, firepoint.position);
            linerend.SetPosition(1, hitInfo.point);

        }
        else
        {
            linerend.SetPosition(0, firepoint.position);
            linerend.SetPosition(1, firepoint.position + firepoint.right * 100);
        }

        if (hitInfo.transform.name == "jumppad" && isJumping == false)
        {
            Invoke("Jump", 1f);
        }
    }

    void Jump()
    {
        isJumping = true;
        rbForPlayer.AddForce(new Vector2(0, jumpHeight));
    }
}
Andre Kampling
  • 5,476
  • 2
  • 20
  • 47
  • You need to check if hitInfo.collider != null. per documentation *This function returns a RaycastHit object with a reference to the Collider that is hit by the ray (the Collider property of the result will be NULL if nothing was hit)* – hijinxbassist Jun 23 '21 at 19:58
  • @Cᴏʀʏ hitInfo is a struct, thus can never be null. – hijinxbassist Jun 23 '21 at 19:59
  • 1
    `if (hitInfo)` works because the `RaycastHit2D` struct contains `implicit operator bool`. Nevertheless his failing code line is not inside that check. – Andre Kampling Jun 23 '21 at 20:17
  • @hijinxbassist as mentioned in the comment above in later versions they added an [implicit operator bool](https://github.com/Unity-Technologies/UnityCsReference/blob/61f92bd79ae862c4465d35270f9d1d57befd1761/Modules/Physics2D/ScriptBindings/Physics2D.bindings.cs#L2856) which does exactly that check `return hit.collider != null;` ... but yes according line is simply outside the check so it is executed regardless of a hit or not – derHugo Jun 24 '21 at 10:20

1 Answers1

0

You need to do the following null check to check if it hits something.

// If it hits something...
if (hitInfo.collider != null)
    ...

It is written in the documentation of Physics2D.Raycast. See also the collider property of the RaycastHit2D struct.

You can also write:

if (hitInfo)
    ...

At first it looks like that this cannot compile, because hitInfo is a struct of type RaycastHit2D and cannot converted to a bool.

But it contains the following code, see here on GitHub:

public static implicit operator bool (RaycastHit2D hit)
{
    return hit.collider != null;
}

You already wrote if (hitInfo), but the line causing the error is not included in that check.

Andre Kampling
  • 5,476
  • 2
  • 20
  • 47