Hi, I am trying to play an animation called swing that plays when I hit the left mouse button to kill an enemy. And then it plays the idle animation. But, I keep getting the error -
NullReferenceException: Object reference not set to an instance of an object
Player.Update () (at Assets/Scripts/Character/Player.cs:35)
What am I doing wrong?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Animations;
public class Player : MonoBehaviour
{
public float maxHealth, maxThirst, maxHunger;
public float thirstIncreaseRate, hungerIncreaseRate;
private float health, thirst, hunger;
public bool dead;
public GameObject player;
public float damage;
public bool weaponEquipped;
public static bool triggeringWithAI;
public static GameObject triggeringAI;
private Animation anim;
public string swingAnim;
public string idleAnim;
public bool swinging;
public void Start()
{
health = maxHealth;
}
public void Update()
{
if (swinging == true)
anim.Play(swingAnim);
else
anim.Play(idleAnim);
if (!dead)
{
hunger += hungerIncreaseRate * Time.deltaTime;
thirst += thirstIncreaseRate * Time.deltaTime;
}
if (thirst >= maxThirst)
Die();
if (hunger >= maxHunger)
Die();
//print(thirst);
//print(hunger);
//print(health);
//detecting and killing ai
if(triggeringWithAI == true && triggeringAI)
{
if (Input.GetMouseButtonDown(0))
{
swinging = true;
Attack(triggeringAI);
}
}
if (!triggeringAI)
triggeringWithAI = false;
}
public void Attack(GameObject target)
{
if(target.tag == "Animal" && weaponEquipped)
{
Animal animal = target.GetComponent<Animal>();
animal.health -= damage;
}
}
public void Die()
{
dead = true;
Destroy(player);
print("You died.");
}
public void Drink(float decreaseRate)
{
thirst -= decreaseRate;
}
public void Eat(float decreaseRate)
{
hunger -= decreaseRate;
}
public void OnTriggerEnter(Collider other)
{
if(other.tag == "Animal")
{
triggeringAI = other.gameObject;
triggeringWithAI = true;
}
}
public void OnTriggerExit(Collider other)
{
if (other.tag == "Animal")
{
triggeringAI = null;
triggeringWithAI = false;
}
}
}