Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

Trying to increase score when enemy is hit

$
0
0
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.

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>