I get this error whenever I press 5: NullReferenceException:
Object reference not set to an instance of an object
WarriorSpin.Update () (at Assets/Scripts/SpellsAndAbilities/Warrior/WarriorSpin.cs:30)
I have checked to make sure everything is the way it is supposed to be but it still doesn’t work.
Here is my code:
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class WarriorSpin : MonoBehaviour {
public GameObject player;
public float coolDown;
PlayerScript ps;
public GameObject canvas;
public GameObject errorMessageArea;
void Start () {
player = gameObject;
ps = player.GetComponent<PlayerScript> ();
canvas = GameObject.Find ("HudCanvas");
errorMessageArea = GameObject.Find ("ErrorMessageArea");
}
void Update () {
if (Input.GetButtonDown ("5")) {
if (coolDown <= 0) {
if (ps.mana > 50) {
ps.isWarriorSpin = true;
coolDown = 30;
ps.mana -= 50;
} else {
GameObject notEnoughManaObject = Instantiate (ps.notEnoughMana, errorMessageArea.transform.position, errorMessageArea.transform.rotation) as GameObject;
notEnoughManaObject.transform.SetParent (canvas.gameObject.transform);
}
} else {
GameObject notReadyYetObject = Instantiate (ps.notReadyYet, errorMessageArea.transform.position, errorMessageArea.transform.rotation) as GameObject;
notReadyYetObject.transform.SetParent (canvas.gameObject.transform);
}
}
if (GetComponent<Text> () != null && coolDown > 1) {
GetComponent<Text> ().text = "" + coolDown.ToString ("0");
}
if (GetComponent<Text> () != null && coolDown > 0 && coolDown <= 1) {
GetComponent<Text> ().text = "" + coolDown.ToString ("F1");
}
if (GetComponent<Text> () != null && coolDown <= 0) {
GetComponent<Text> ().text = "";
}
coolDown -= Time.deltaTime;
}
}