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