- Home /
Scripting Help with My Projectile System
Hi. I am currently making an FPS game, and I made a simple shooting script and projectile script. However, when I shoot at the skybox, I get a Null Reference Exception error since my hit
variable returns null each time I shoot a ray to the skybox. My Projectile and Shooting script are shown below:
Projectile Script:
using UnityEngine;
public class Projectile : MonoBehaviour
{
private float _speed;
private Vector3 _target;
public void StartMovingTowards(Vector3 target, float speed)
{
_target = target;
_speed = speed;
}
public void FixedUpdate()
{
transform.position = Vector3.MoveTowards(transform.position, _target, _speed);
}
}
Shooting Script:
using UnityEngine;
public class Shooting : MonoBehaviour
{
public float damage = 10f;
public float range = 100f;
public float fireRate = 1f;
public float impactForce = 30f;
public float projectileSpeed = 1f;
public Camera fpsCam;
public Transform firePoint;
public GameObject projectile;
private RaycastHit hit;
private float nextTimeToFire = 0f;
private void Update()
{
if (Input.GetButton("Fire1") && nextTimeToFire <= Time.time)
{
nextTimeToFire = Time.time + 1f / fireRate;
Shoot();
// Every Time I shoot, I want to Instantiate the bullet and move it
GameObject proj = Instantiate(projectile, firePoint);
var movement = proj.GetComponent<Projectile>();
movement.StartMovingTowards(hit.transform.position, projectileSpeed);
}
}
void Shoot()
{
if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
{
Debug.Log(hit.transform.name);
Target target = hit.transform.GetComponent<Target>();
if (target != null)
{
target.TakeDamage(damage);
}
if (hit.rigidbody != null)
{
hit.rigidbody.AddForce(-hit.normal * impactForce);
}
}
}
}
So what I want is that even if I'm facing the skybox, the bullet will fire. However, that doesn't happen, since hit = null
when I'm facing the skybox since the skybox isn't a GameObject with a collider. So I do know why the error is coming, but I do not know how to tweak the code to perform how I want it. Thanks to anyone who answers.
Answer by CoreDLL · Mar 13, 2021 at 10:28 PM
Hit is null by default when declared, then you check for Raycast if it hits something the Hit will be changed from null to a reference of a Hit and you can use its information, but if Raycast doesn't hit anything the Hit variable will remain null and the else part of the if statement will be executed, problem is that you use the Hit variable in the else part when raycast didn't hit anything and Hit is null. error is at row 51 movement.StartMovingTowards(hit.transform.position, projectileSpeed); you try to use position of a Hit that doesnt exist.
First of all, thanks for your answer. However, how do I fix this then? Instead of checking the Raycast, should I check if (hit != null)
, then do the Raycast? Because I already tried that and that didn't work
if the raycast doesn't hit anything he will let the Hit variable to be null, and you need to make sure that you don't use Hit when is null. You can do this by checking if Hit == null before use or use the if where you made the Raycast:
Raycast Hit; {null by default}
if(Physics.Raycast(........out Hit))
{
{Raycast hit something}
{ you can use Hit there because Raycast hit something and Hit is not null anymore}
}
else
{
{Raycast didn't hit anything }
{ you cannot use Hit here because Raycast didnt hit anything and Hit remained null }
}
Sorry, I don't think I asked my question that well.
What I want is that the bullet will shoot, every time I hit my left mouse button. So what I did was that I made a Raycast, and if the Raycast collides with an object with a collider on it, my bullet will shoot. However, I noticed that my bullet won't shoot if I try ai$$anonymous$$g at the sky, (or nothing), since hit = null and I'll get a Null Reference Exception error. So I get why my error is co$$anonymous$$g. What I don't know, is how to fix it. Again, I want my bullet to shoot, even if I'm facing the skybox. However, I can't, since hit will be equal to null if I aim at the skybox.