- Home /
Question by
Venator · Mar 13, 2014 at 06:59 AM ·
errorgameobjectnullreferenceexceptionhealth
Error when player trying to take damage from a hazard
Hi guys, I am currently working on a game that includes a health system. As part of the game, when the player runs into a saw they take damage, managed by an already set-up "modifyHealth" action. However, when the player collides with the saw, I instead get the error:
"NullReferenceException: Object reference not set to an instance of an object SawBlade.OnTriggerEnter (UnityEngine.Collider c) (at Assets/Scripts/SawBlade.cs:14)"
Here is the code in question:
using UnityEngine;
using System.Collections;
public class SawBlade : MonoBehaviour {
public float speed = 300;
void Update () {
transform.Rotate (Vector3.forward * speed *Time.deltaTime, Space.World);
}
void OnTriggerEnter(Collider c) {
if (c.tag == "Player") {
c.GetComponent<Health>().modifyHealth(-10);
}
}
}
Does anyone know what could possibly be the problem?
Thanks!
Comment
Are you absolutely sure that every object with a 'Player' tag has a 'Health' script attached? And it must be the same game object that has the collider, not a parent or child.