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

NullReferenceException: Object reference not set to an instance of an object

$
0
0
I have come across a lot of people that have had this error, however I haven't been able to fix the error myself. I am simply trying to have an enemy collider deal damage to my player. Player: public class PlayerHealth : MonoBehaviour { public int maxHealth = 100; private int currentHealth; void Awake() { currentHealth = maxHealth; } void Start () { print (currentHealth); } void OnCollisionEnter(Collision collision) { if (collision.gameObject.tag == "Enemy") { currentHealth -= GetComponent ().enemyDamage; //currentHealth -= GetComponent ().enemyDamage; Debug.Log("You have been hit!" + currentHealth); } } } And for the enemy: public class EnemyStats : MonoBehaviour { public int maxHealth = 100; private int currentHealth; public int enemyDamage = 10; public int armorRating = 10; void Awake() { currentHealth = maxHealth; } public void Damage(int gunDamageAmount) { currentHealth -= gunDamageAmount - armorRating; if (currentHealth <= 0f) { gameObject.SetActive (false); Debug.Log ("Enemy killed!"); } } }

Viewing all articles
Browse latest Browse all 2891

Trending Articles



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