This question was
closed Oct 12, 2015 at 05:39 AM by
H4L3Z for the following reason:
Omg, went full retard ahah sorry. The issue was an enemy tag accidentally placed on the wrong object.
Question by
H4L3Z · Oct 12, 2015 at 12:57 PM ·
c#errornullreferenceexception
Not sure why i'm getting NullReferenceException
Okay so here is my code and for some reason it's working the way I intended yet still returns an error :/ Can anyone please explain how to fix this?
Code:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour {
public int playerHealth = 20;
void Start ()
{
}
void Update ()
{
if(playerHealth <= 0)
{
Debug.Log("player died");
}
}
void OnCollisionEnter2D(Collision2D other)
{
if (other.gameObject.tag == "Enemy")
{
playerHealth -= other.gameObject.GetComponent<ChargerAI>().attackDamage;
}
}
}
Error:
NullReferenceException: Object reference not set to an instance of an object
Player.OnCollisionEnter2D (UnityEngine.Collision2D other) (at Assets/Scripts/Player.cs:25)
Comment
Follow this Question
Related Questions
Null Reference Exception For An Unknown Reason 0 Answers
Null Reference in UnityStandardAssets.Utility.WaypointProgressTracker.Update 0 Answers
There's an issue with this script I can't find 0 Answers
Random Chance NullReferenceException: Object reference not set to an instance of an object 2 Answers