i am making a zombie game, but i keep getting an error about an object not set, and wont add the damage to the player.
her is the error:
NullReferenceException: Object
reference not set to an instance of an
object ZombieAttack.Update () (at
Assets/Scripts/Zombie/ZombieAttack.cs:22)
but it is assigned and working like it should, exept for the health going down.
here is the code attached to the player:
using UnityEngine;
using System.Collections;
public class ZombieAttack : MonoBehaviour
{
public GameObject[] enemies;
LifeScript life;
void Start()
{
life = GetComponent<LifeScript>();
enemies = GameObject.FindGameObjectsWithTag("zombie");
}
void Update()
{
enemies = GameObject.FindGameObjectsWithTag("zombie");
for (int i = 0; i < enemies.Length; i++)
{
Debug.Log(i);
if (enemies*.GetComponent<ZombieWalking>().canAttack == true)*
{
life.health -= enemies*.GetComponent().dam;*
enemies*.GetComponent().canAttack = false;*
}
}
}
}
and here is the code the script is referencing on the zombie:
using UnityEngine;
using System.Collections;
public class ZombieWalking : MonoBehaviour {
* public float speed;*
* public GameObject player;*
* public float distanceToPlayer;*
public float cooldown;
public bool canAttack = false;
public int dam = 6;
* // Use this for initialization*
* void Start () {*
* player = GameObject.FindGameObjectWithTag (“rick”);*
* }*
* // Update is called once per frame*
* void Update () {*
* distanceToPlayer = Vector3.Distance (transform.position, player.transform.position);*
* if (distanceToPlayer > 1.6f && distanceToPlayer < 10.0f) {*
* transform.position = Vector3.MoveTowards (transform.position, player.transform.position, speed);*
transform.LookAt(player.transform.position);
* }*
if (distanceToPlayer < 2)
{
if (!canAttack)
{
cooldown -= Time.deltaTime;
if (cooldown < 0.1f)
{
canAttack = true;
cooldown = 3;
}
}
}
if (distanceToPlayer > 2)
{
canAttack = false;
cooldown = 3;
}
* }*
}
please help if you can