- Home /
This question was
closed Jan 09, 2015 at 11:29 AM by
Graham-Dunnett for the following reason:
Duplicate Question
Question by
yvyv1000 · Jan 09, 2015 at 11:28 AM ·
errorraycastnullreferenceexceptionshooting
raycast shooting
hello guys i have a script with a raycast shooting part in it but every time i use it it gives the error: NullReferenceException: Object reference not set to an instance of an object MoveTank.Update () (at Assets/P3J/Demo Scene/MoveTank.js:129)
my script:
#pragma strict
var leftTrack : MoveTrack;
var rightTrack : MoveTrack;
var acceleration : float = 5;
var currentVelocity : float = 0;
var maxSpeed : float = 25;
var rotationSpeed : float = 30;
var spawnPoint : Transform;
var bulletObject : GameObject;
var fireEffect : GameObject;
var maxShots : int = 1 ;
var shots : int = 0 ;
public var ReloadTime:float = 2;
var MaxAmmo : int = 40 ;
var gunSnd : AudioClip;
var Effect : Transform;
var TheDammage = 100;
function Start() {
// Get Track Controls
leftTrack = GameObject.Find(gameObject.name + "/Lefttrack").GetComponent(MoveTrack);
rightTrack = GameObject.Find(gameObject.name + "/Righttrack").GetComponent(MoveTrack);
}
function Update () {
if (Input.GetKey (KeyCode.W)) {
// plus speed
if (currentVelocity <= maxSpeed)
currentVelocity += acceleration * Time.deltaTime;
} else if (Input.GetKey (KeyCode.S)) {
// minus speed
if (currentVelocity >= -maxSpeed)
currentVelocity -= acceleration * Time.deltaTime;
} else {
// No key input.
if (currentVelocity > 0)
currentVelocity -= acceleration * Time.deltaTime;
else if (currentVelocity < 0)
currentVelocity += acceleration * Time.deltaTime;
}
// Turn off engine if currentVelocity is too small.
if (Mathf.Abs(currentVelocity) <= 0.05)
currentVelocity = 0;
// Move Tank by currentVelocity
transform.Translate(Vector3(0, 0, currentVelocity * Time.deltaTime));
// Move Tracks by currentVelocity
if (currentVelocity > 0) {
// Move forward
leftTrack.speed = currentVelocity;
leftTrack.GearStatus = 1;
rightTrack.speed = currentVelocity;
rightTrack.GearStatus = 1;
}
else if (currentVelocity < 0) {
// Move Backward
leftTrack.speed = -currentVelocity;
leftTrack.GearStatus = 2;
rightTrack.speed = -currentVelocity;
rightTrack.GearStatus = 2;
}
else {
// No Move
leftTrack.GearStatus = 0;
rightTrack.GearStatus = 0;
}
// Turn Tank
if (Input.GetKey (KeyCode.A)) {
if (Input.GetKey(KeyCode.DownArrow)) {
// Turn right
transform.Rotate(Vector3(0, rotationSpeed * Time.deltaTime, 0));
leftTrack.speed = rotationSpeed;
leftTrack.GearStatus = 1;
rightTrack.speed = rotationSpeed;
rightTrack.GearStatus = 2;
} else {
// Turn left
transform.Rotate(Vector3(0, -rotationSpeed * Time.deltaTime, 0));
leftTrack.speed = rotationSpeed;
leftTrack.GearStatus = 2;
rightTrack.speed = rotationSpeed;
rightTrack.GearStatus = 1;
}
}
if (Input.GetKey (KeyCode.D)) {
if (Input.GetKey(KeyCode.DownArrow)) {
// Turn left
transform.Rotate(Vector3(0, -rotationSpeed * Time.deltaTime, 0));
leftTrack.speed = rotationSpeed;
leftTrack.GearStatus = 2;
rightTrack.speed = rotationSpeed;
rightTrack.GearStatus = 1;
} else {
// Turn right
transform.Rotate(Vector3(0, rotationSpeed * Time.deltaTime, 0));
leftTrack.speed = rotationSpeed;
leftTrack.GearStatus = 1;
rightTrack.speed = rotationSpeed;
rightTrack.GearStatus = 2;
}
}
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(Vector3(Screen.width*0.5, Screen.height*0.5, 0));
// Fire!
if (Input.GetButtonDown("Fire1") && shots < maxShots && MaxAmmo > 0) {
if (Physics.Raycast (ray, hit, 100))
{
var particleClone = Instantiate(Effect, hit.point, Quaternion.LookRotation(hit.normal));
Destroy(particleClone.gameObject, 2);
hit.transform.SendMessage("ApplyDammage", TheDammage, SendMessageOptions.DontRequireReceiver);
}
shots ++;
Reload();
MaxAmmo --;
}
}
function Reload(){
yield WaitForSeconds(ReloadTime);///Wait 2(ReloadTime) seconds before continuing
shots = 0;
}
function OnGUI () {
GUI.Label (Rect (750, 25, 100, 30), MaxAmmo.ToString() + " Shells left");
GUI.Label (Rect (750, 50, 150, 30), ReloadTime.ToString() + " Sec reload time");
}
Comment