I have a game where you hit and enemy with an arrow, it takes 1 damage which reduces its health by 1. now i, trying to add a score system so when you hit the enemy you also gain a point. But with my current code i an getting "NullReferenceException: Object reference not set to an instance of an object".
This script is attached to the text object i have on the canvas:
public class PlayerScore : MonoBehaviour
{
public int score = 0;
void Update ()
{
GetComponent().text = "Score: " + score;
}
}
And this script is on the enemy:
public class ArrowDamage : MonoBehaviour {
private PlayerScore score;
void Awake ()
{
score = GameObject.Find("ScoreText").GetComponent();
}
void OnTriggerEnter (Collider Enemy)
{
if (Enemy.gameObject.CompareTag("Enemy"))
{
Enemy.GetComponent().Health --;
Enemy.GetComponent().score ++;
}
}
}
The error refers to Enemy.GetComponent().score ++;
I have a feeling the problem is that it is trying to find PlayerScore on the Enemy rather than the object with the text on it but i'm not sure what to do.
↧