- Home /
Raycasting error
Hello Everyone! I am trying to learn some basic raycasting to make some bullet stuff. The Player script has a public method that returns true if a ray directly infront of the camera hits something. It also outs a RaycastHit variable. Within the weapon script, I am trying to make it use that function to detect if the ray hit something. If not, it creates a bullet object (bullet objects don't actually do anything, they are there as a visual queue so that the player knows they have shot something) directly from the gun going forward at the max distance that the gun shoots. If the ray does hit something and returns true, the weapon script will make the bullet angle toward the hit point so that it looks more accurate without getting infront of the player's face and blocking the camera. But I am having a weird issue with it saying...
"Object reference not set to an instance of an object"
...when it calls the sendRay() method.
Here is the scripts...
First, the player script, which is a component of the main camera.
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public bool sendRay(out RaycastHit hit, float maxDist){
Ray shot = new Ray(transform.position, transform.forward);
if (Physics.Raycast(shot, out hit, maxDist)) return true;
else return false;
}
}
here is the weapon script, on a component of an object that is a child of the main camera.
using UnityEngine;
using System.Collections;
public class MachineGun : MonoBehaviour {
public float fireDelay = 0.2f;
public int damage = 1;
public float maxDistance = 10.0f;
float currentDelay = 0.0f;
public GameObject bullet;
// Update is called once per frame
void Update () {
currentDelay -= Time.deltaTime;
if(currentDelay < 0.0f)
currentDelay = 0.0f;
if(Input.GetAxis("Fire1") == 1.0f && currentDelay == 0.0f){
GameObject bul = Instantiate(bullet, transform.position, transform.rotation) as GameObject;
bul.GetComponent<Bullet>().setDistance( maxDistance );
RaycastHit hit;
if( transform.parent.GetComponent<Player>().sendRay(out hit, maxDistance) ){
bul.transform.LookAt(hit.point);
bul.GetComponent<Bullet>().setDistance( 1.0f );
if(hit.collider.tag == "Enemy"){
hit.collider.gameObject.GetComponent<Health>().hurt(damage);
}
}
}
}
}
The specific error is:
NullReferenceException: Object reference not set to an instance of an object MachineGun.Update () (at Assets/Custom/Weapons/MachineGun.cs:25)
What do you think would cause this problem, and how can I fix it?
This is not a raycasting error but a null reference error and you can find many similar questions. Check the stuff used on line 26 (the line the error occurs on) against null.
@ $$anonymous$$accabbe; I think I got that far. I just don't know what to do to solve it. I think it might be the fact that a method can't use an outed variable as a parameter until it has a value? I don't really know, I am new to C#.
@ Chris333; Yes, there is a prefab object assigned there.
I also want to mention, there was a glitch that was making the bullet fire on every frame ins$$anonymous$$d of the actual fire rate. Although that is unrelated to this problem, I did fix it.
Could you try this:
if(transform.parent.gameObject.GetComponent().sendRay(out hit, maxDistance) )
in line 25.
Answer by Bodaciouslycrazy · Apr 24, 2015 at 03:51 AM
OH my gosh, I feel like an idiot. I wasted everyone's time... I made this player script, but completely forgot to actually place it on the main character. Such a stupid mistake. I thought it had to do with "out" variables... Thank you for helping though! I am learning a lot!