So, i have this problem that I really don’t know how to fix.
Basicly, a prefab is istantiated on the game using this script:
var Zombie : Transform;
private var time = 300;
var Spawner : Transform;
function Istantiate()
{
Instantiate(Zombie, Spawner.position, Quaternion.identity);
}
function Update(){
time +=1;
if(time > 300){
Istantiate();
time = 0;
}
}
and it works pretty well, then I have this Enemy AI script
var VitaNemico = 300;
var velocity = 0.3;
var Player : Transform;
function Start () {
Player = GameObject.FindWithTag("Player").transform;
}
function Damage() {
VitaNemico -=10;
}
function Run() {
transform.position = Vector3.MoveTowards(transform.position, Player.position, velocity);
transform.LookAt(Player);
}
function Die() {
Destroy (gameObject);
}
function Update() {
Run();
if (VitaNemico == 0){
Die();
}
}
And again, it works pretty well.
but then, I use this Gun script:
var hit : RaycastHit;
public var otherClip: AudioClip;
private var Clip = 30;
private var Ammo = 10;
private var time = 60;
var Ammotex : Texture2D;
var enemy : GameObject;
function Start(){
enemy = GameObject.FindWithTag("Zombie");
}
function shoot()
{
if(Input.GetButtonDown("Fire1"))
if(time > 60)
{
audio.clip = otherClip;
audio.Play();
Clip -=1;
time = 0;
if(Physics.Raycast(transform.position, transform.forward, hit, range))
{
if(hit.collider.transform.tag == "Zombie")
{
return true;
}
else
{
return false;
}
}
}
}
function Update()
{
time +=1;
if(shoot() == true)
{
print("HIT");
enemy.GetComponent(EnemyAI).Damage();
GameObject.FindWithTag("Player").GetComponent(ScoreManager).AddScore();
}
if(Clip == 0){
Reload();
}
}
function Reload()
{
yield WaitForSeconds (1);
Clip = 30;
Ammo =9;
}
function OnGUI () {
GUI.Label (Rect (900,60,20,20), Ammotex);
GUI.Box (Rect (915,60,25,25), Clip.ToString());
GUI.Box (Rect (940,60,25,25), Ammo.ToString());
}
and It works well too, but when I hit the zombie and it should use:
enemy.GetComponent(EnemyAI).Damage();
there’s a NullReferenceException, what should I do?