So I have script that is my raycast shooting script. Here is the script:
var bulletTex : GameObject[];
var Effect : Transform;
var TheDammage = 100;
var fireRate : float = 0.5;
private var nextFire : float = 0.0;
var gunShot : AudioClip;
public var spreadFactor : float = 0.03;
@HideInInspector
public var spreadFactorOld : float = 0;
var muzzleFlash : Renderer;
var muzzleLight : Light;
var gunShotAnimation : boolean;
function Start() {
muzzleFlash.enabled = false;
muzzleLight.enabled = false;
}
function Update () {
Fire();
}
function Fire () {
if(Input.GetMouseButton(0) && Time.time > nextFire) {
nextFire =Time.time + fireRate;
ForceFire();
AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
}
}
function ForceFire () {
var fwd = transform.TransformDirection(Vector3.forward);
fwd.x += Random.Range(-spreadFactor, spreadFactor);
fwd.y += Random.Range(-spreadFactor, spreadFactor);
fwd.z += Random.Range(-spreadFactor, spreadFactor);
var hit : RaycastHit;
Debug.DrawRay(transform.position, fwd * 10, Color.green);
if (Input.GetMouseButton(0))
{
if (Physics.Raycast (transform.position, fwd, hit, 100))
{
var bulletHole = Instantiate(bulletTex[Random.Range(0,1)], hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal));
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
Destroy(bulletHole.gameObject, 10);
hit.transform.SendMessage("ApplyDammage", 10, SendMessageOptions.DontRequireReceiver);
AudioSource.PlayClipAtPoint(gunShot, transform.position, 1);
Shoot();
gunShotAnimation = true;
yield WaitForSeconds(0.01);
gunShotAnimation = false;
}
}
}
function Shoot()
{
muzzleFlash.renderer.enabled = true;
muzzleLight.enabled = true;
yield WaitForSeconds(0.02);
muzzleFlash.renderer.enabled = false;
muzzleLight.enabled = false;
}
At the top is the variable gunShotAnimation. When the raycast hits I have that variable become true and then after 0.01 seconds become false. The arms script is the script that is supposed to acces the gunShotAnimation variable. Here is the arms script:
#pragma strict
var AK47 : GameObject;
internal var animator : Animator; //stores the animator component
var armsRunning : boolean;
var punchRight : boolean;
var punchLeft : boolean;
var AK47Idle : boolean;
var AK47GunShot : boolean;
var playerGunShot : RaycastShootingMachineGun;
function Start () {
animator = GetComponent(Animator); //assigns Animator component when we start the game
}
function Update () {
Other();
}
function FixedUpdate () {
animator.SetBool ("ArmsRunning", armsRunning);
animator.SetBool ("PunchRight", punchRight);
animator.SetBool ("PunchLeft", punchLeft);
animator.SetBool ("AK47Idle", AK47Idle);
animator.SetBool ("AK47GunShot", AK47GunShot);
}
function Other () {
if (Input.GetKeyDown("left shift"))
{
armsRunning = true;
}
if (Input.GetKeyUp("left shift"))
{
armsRunning = false;
}
if (Input.GetMouseButtonDown(0))
{
punchLeft = true;
}
if (Input.GetMouseButtonUp(0))
{
punchLeft = false;
}
if (Input.GetMouseButtonDown(1))
{
punchRight = true;
}
if (Input.GetMouseButtonUp(1))
{
punchRight = false;
}
if (AK47.active == true)
{
AK47Idle = true;
}
if (AK47.active == false)
{
AK47Idle = false;
}
if (playerGunShot.gunShotAnimation == true)
{
AK47GunShot = true;
}
}
So in the arms script at the top I call the raycast script with var playerGunShot : RaycastShootingMachineGun; Then at the bottom I say if the gunShotAnimation is true, then set AK47GunShot to true. When AK47GunShot is true then the AK47 gunshot animation will play.
When I hit play everything works except, of course, the gun shot animation. I get this error “NullReferenceException: Object reference not set to an instance of an object” on the line - if (playerGunShot.gunShotAnimation == true) - so basically it is saying that something doesn’t exist. For some reason it isn’t recognizing my gunShotAnimation variable.
I don’t know why it is doing this. I could be writing it wrong but I don’t know any other way to write the script. I think it’s because I am writing it wrong so please if anybody can help me with this that would be great.
Thanks in advance!!!