- Home /
Shoot Script not working correctly
I was following this tutorial for a shoot script : LINK. I copied the script and I am having two errors. The first error is that the BulletsLeft doesnt go down when I shoot. The second error says:
NullReferenceException: Object reference not set to an instance of an object Shoot.Shot () (at Assets/Scripts/Shoot.js:71) Shoot.Update () (at Assets/Scripts/Shoot.js:47)
Line 71 is Debug.DrawRay(transform.posistion , DirectionRay * Range , Color.red);
Here is my Code
var Range : float = 1000;
var Force : float = 1000;
var Clips : int = 2;
var BulletsPerClip : int = 16;
var ReloadTimer : float = 0;
var ReloadCooler : float = 3.3;
var Damage : int = 10;
var BulletsLeft : int = 16;
var ShootTimer : float = 0;
var ShootCooler : float = 0.09;
public var ShootSound : AudioClip;
public var ReloadSound : AudioClip;
public var EmptySound : AudioClip;
var HitParticles : ParticleEmitter;
function Start ()
{
BulletsLeft = BulletsPerClip;
HitParticles.emit = false;
}
function Update ()
{
if(ReloadTimer > 0){
ReloadTimer -= Time.deltaTime;
}
if(ReloadTimer < 0){
ReloadTimer = 0;
}
if(ShootTimer > 0){
ShootTimer -= Time.deltaTime;
}
if(ShootTimer < 0){
ShootTimer = 0;
}
if(BulletsLeft < 0){
BulletsLeft = 0;
}
if(Input.GetMouseButtonDown(0) && ShootTimer == 0)
{
if(BulletsLeft > 0)
{
if(ShootTimer == 0)
{
PlayShootSound();
ShootTimer = ShootCooler;
Shot();
BulletsLeft --;
}
}
if(BulletsLeft == 0 && Clips == 0)
{
if(ShootTimer == 0)
{
PlayEmptySound();
ShootTimer = ShootCooler;
}
}
if( BulletsLeft == 0 && Clips > 0 && ReloadTimer == 0)
{
PlayReloadSound();
Reload();
}
}
}
function Shot ()
{
var Hit : RaycastHit;
var DirectionRay = transform.TransformDirection(Vector3.forward);
Debug.DrawRay(transform.posistion , DirectionRay * Range , Color.red);
if(Physics.Raycast(transform.posistion , DirectionRay , Hit , Range))
{
if(Hit.rigidbody)
{
if(HitParticles)
{
HitParticles.transform.posistion = Hit.point;
HitParticles.transform.localRotation = Quaternion.FromToRotation(Vector3.forward, Hit.normal);
HitParticles.Emit ();
Hit.rigidbody.AddForceAtPosition(DirectionRay * Force , Hit.point);
Hit.collider.SendMessageUpwards ("ApplyDamage" , Damage, SendMessageOptions.DontRequireReciever);
}
}
}
}
function Reload()
{
ReloadTimer = ReloadCooler;
yield WaitForSeconds(ReloadTimer);
BulletsLeft = BulletsPerClip;
Clips -=1;
}
function PlayShootSound ()
{
audio.PlayOneShot (ShootSound);
}
function PlayReloadSound ()
{
audio.PlayOneShot (ReloadSound);
}
function PlayEmptySound ()
{
audio.PlayOneShot (EmptySound);
}
And in function shoot try BulletsLeft --; this will subtract 1 each time you shoot.
Answer by Joyrider · Aug 12, 2013 at 02:47 PM
Euh, you wrote posistion everywhere instead of position...
And on a sidenote, your if(ShootTimer == 0)
-statements on line 43 and 53 will always be true, since you already checked this in the if-statement enclosing these: if(Input.GetMouseButtonDown(0) && ShootTimer == 0)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How do I modify my script to give my gun recoil when I shoot? 1 Answer
BCE0005: Unknown identifier: 'DirectionRay'. 1 Answer
Problem doing a script. 2 Answers