- Home /
Raycast causes game to crash when shooting up
Okay, so right now I have a gun script that raycasts directly in front of the camera when the character shoots. Right now, it works fine when I shoot at something, but if I just look up and try to shoot at the sky the game crashes and says "NullReferenceException: Object reference not set to an instance of an object" even though I've used the same script several times without a problem. Here's what I have so far:
var direction = this.transform.TransformDirection(Vector3.forward);
var RayCastHit : RaycastHit;
if (Physics.Raycast (transform.position, direction, RayCastHit, FireRange))
{
if (RayCastHit.rigidbody)
{
if (Weapon == "G36C")
{
RayCastHit.rigidbody.AddForceAtPosition(150 * direction, RayCastHit.point);
}
}
}
if (RayCastHit.collider.gameObject.tag == "metal")
{
MetalParticle1.transform.position = RayCastHit.point;
MetalParticle2.transform.position = RayCastHit.point;
MetalParticle3.transform.position = RayCastHit.point;
MetalParticle4.transform.position = RayCastHit.point;
MetalParticle5.transform.position = RayCastHit.point;
MetalParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
MetalParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
MetalParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
MetalParticle4.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
MetalParticle5.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
MetalParticle1.Emit();
MetalParticle2.Emit();
MetalParticle3.Emit();
MetalParticle4.Emit();
MetalParticle5.Emit();
MetalParticle1.audio.Play();
}
else if (RayCastHit.collider.gameObject.tag == "concrete")
{
ConcreteParticle1.transform.position = RayCastHit.point;
ConcreteParticle2.transform.position = RayCastHit.point;
ConcreteParticle3.transform.position = RayCastHit.point;
ConcreteParticle4.transform.position = RayCastHit.point;
ConcreteParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
ConcreteParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
ConcreteParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
ConcreteParticle4.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
ConcreteParticle1.Emit();
ConcreteParticle2.Emit();
ConcreteParticle3.Emit();
ConcreteParticle4.Emit();
ConcreteParticle1.audio.Play();
}
else if (RayCastHit.collider.gameObject.tag == "wood")
{
WoodParticle1.transform.position = RayCastHit.point;
WoodParticle2.transform.position = RayCastHit.point;
WoodParticle3.transform.position = RayCastHit.point;
WoodParticle4.transform.position = RayCastHit.point;
WoodParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
WoodParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
WoodParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
WoodParticle4.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
WoodParticle1.Emit();
WoodParticle2.Emit();
WoodParticle3.Emit();
WoodParticle4.Emit();
WoodParticle1.audio.Play();
}
else if (RayCastHit.collider.gameObject.tag == "water")
{
WaterParticle1.transform.position = RayCastHit.point;
WaterParticle2.transform.position = RayCastHit.point;
WaterParticle3.transform.position = RayCastHit.point;
WaterParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
WaterParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
WaterParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
WaterParticle1.Emit();
WaterParticle2.Emit();
WaterParticle3.Emit();
}
else
{
DirtParticle1.transform.position = RayCastHit.point;
DirtParticle2.transform.position = RayCastHit.point;
DirtParticle3.transform.position = RayCastHit.point;
DirtParticle1.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
DirtParticle2.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
DirtParticle3.transform.rotation = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal);
DirtParticle1.Emit();
DirtParticle2.Emit();
DirtParticle3.Emit();
DirtParticle1.audio.Play();
}
When I double click the debug thing at the bottom, it highlights the line that says "if (RayCastHit.collider.gameObject.tag == "metal")".
Any help would be appreciated. Thanks
"Shooting at the sky" means that the member collider in your RaycastHit will remain uninitialized. An uninitialized Collider will not have an attached GameObject or tag. So yes, you have a null reference.
(Posted as a comment because you don't accept answers.)
I have the same code on a different game and it works perfectly fine. What could be causing it not to work?
I just told you what the problem is. When the first if-conditional is reached, the game tries to access RayCastHit.collider.gameObject.tag and finds out that it is non-existent, causing it to crash.
Answer by aldonaletto · Jul 25, 2011 at 04:35 AM
You may have changed something in this script, because you're closing the first if before start checking the hit object's tag, so you're comparing the tags even if Raycast returns false (what happens when you shot the sky). Move the 3rd closing brace to the end of your script. You can also do a lot of optimization, like below (pay attention to the comments):
var direction = this.transform.TransformDirection(Vector3.forward); // transform.forward does the same var RayCastHit : RaycastHit; if (Physics.Raycast (transform.position, direction, RayCastHit, FireRange)) { if (RayCastHit.rigidbody) { if (Weapon == "G36C") { RayCastHit.rigidbody.AddForceAtPosition(150 * direction, RayCastHit.point); } } // } <- move this brace to the end of your script // you can do a lot of optimization caching the rotation and tag: var rotNormal = Quaternion.FromToRotation(Vector3.up, RayCastHit.normal); var hitTag = RayCastHit.collider.tag; // you can access tag from the collider reference if (hitTag == "metal") // use the cached tag to compare { MetalParticle1.transform.position = RayCastHit.point; MetalParticle2.transform.position = RayCastHit.point; MetalParticle3.transform.position = RayCastHit.point; MetalParticle4.transform.position = RayCastHit.point; MetalParticle5.transform.position = RayCastHit.point; MetalParticle1.transform.rotation = rotNormal; // use the calculated rotation MetalParticle2.transform.rotation = rotNormal; MetalParticle3.transform.rotation = rotNormal; MetalParticle4.transform.rotation = rotNormal; MetalParticle5.transform.rotation = rotNormal; MetalParticle1.Emit(); ...
Ahhhh, thank you so much! I actually suspected something like that but I wasn't sure.