- Home /
Health C# script problem.
Hi guys, I keep receiving this: NullReferenceException: Object reference not set to an instance of an object DestroyByContact.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scritps/DestroyByContact.cs:41)
What I am trying to do is to give my player some Health (PlayerHealthAndDmg is Player script)
And allow enemies to provide dmg to player when collision happends (DestroyByContact is attached to Enemy)
I am kinda new to whole game making and programing, so be gentle please :)
Thanks!
Here is code:
using UnityEngine;
using System.Collections;
public class PlayerHealthAndDmg : MonoBehaviour {
public int playerHealth;
public int playerDamage;
void Start ()
{
playerHealth=10;
}
public void UpdatePlayerHealth (int pUdt){
playerHealth = playerHealth + pUdt;
}
public int GetPlayerHealth (){
return playerHealth;
}
}
And second file:
using UnityEngine;
using System.Collections;
public class DestroyByContact : MonoBehaviour {
public GameObject explosion;
public GameObject explosionPlayer;
public int scoreValue;
private GameController gameController;
private PlayerHealthAndDmg playerHealthAndDmg;
void Start()
{
GameObject gameControllerObject = GameObject.FindGameObjectWithTag ("GameController");
if (gameControllerObject != null) {
gameController = gameControllerObject.GetComponent<GameController> ();
}
if (gameControllerObject == null) {
Debug.Log ("Cannot find 'GameController' script");
}
GameObject playerHealthAndDmgObject = GameObject.FindGameObjectWithTag ("Player");
if (playerHealthAndDmg != null) {
playerHealthAndDmg = playerHealthAndDmgObject.GetComponent<PlayerHealthAndDmg> ();
}
if (playerHealthAndDmg == null) {
Debug.Log ("Cannot find 'Player' script");
}
}
void OnTriggerEnter(Collider other) {
if (other.tag == "Boundry")
return;
if(other.tag =="Enemy")
return;
Instantiate(explosion, transform.position, transform.rotation);
if (other.tag == "Player") {
playerHealthAndDmg.UpdatePlayerHealth(-10);
if(playerHealthAndDmg.GetPlayerHealth()<0)
Instantiate (explosionPlayer, other.transform.position, other.transform.rotation);
gameController.GameOver ();
}
gameController.AddScore (scoreValue);
Destroy(other.gameObject);
Destroy (gameObject);
}
}
Answer by ChrisMaster · Jan 09, 2014 at 06:41 PM
Was:
if (playerHealthAndDmg != null) {
Should be:
if (playerHealthAndDmgObject != null)
Now it works ... Kinda :)
Thanks!
Answer by robertbu · Jan 09, 2014 at 05:18 PM
One bug that would account for this behavior is on line 25. You have:
if (playerHealthAndDmg != null) {
But it should be '==':
if (playerHealthAndDmg == null) {
One bug that would account for this behavior is on line 25. You have:
if (playerHealthAndDmg != null) {
But it should be '==':
if (playerHealthAndDmg == null) {
Still not working ... I get this ins$$anonymous$$d when i put you code:
NullReferenceException: Object reference not set to an instance of an object DestroyByContact.Start () (at Assets/Scritps/DestroyByContact.cs:26)
I just chacked debug and : Cannot find 'Player' script UnityEngine.Debug:Log(Object) DestroyByContact:Start() (at Assets/Scritps/DestroyByContact.cs:30)
When using original code, why it cannot find Player - the object is tagged corretly ... ?
Answer by ChrisMaster · Jan 09, 2014 at 06:40 PM
Omg it's alive, ALIVE and it works :o
I found out myself but answer roberbu was kind a hint :) thanks.
WAS:
if (playerHealthAndDmg != null)
SHOULD BE:
if (playerHealthAndDmgObject == null)
its not working corretly, but due to other errors I had to rewrite the end of the code aswell:
if (other.tag == "Player") {
playerHealthAndDmg.UpdatePlayerHealth (-10);
if (playerHealthAndDmg.GetPlayerHealth () < 0) {
Instantiate (explosionPlayer, other.transform.position, other.transform.rotation);
Destroy(other.gameObject);
Destroy (gameObject);
gameController.GameOver ();
}
return;}
Your answer
![](https://koobas.hobune.stream/wayback/20220613125947im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Enemy Health Damage 2 Answers
Problem with health bar 1 Answer
health problem 1 Answer
Call a Void on Collision 2 Answers
Problems with buttons all turning on or off at the same time 2 Answers