Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Mar 19, 2016 at 12:35 PM by Cazo0 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Cazo0 · Mar 14, 2016 at 08:19 PM · errornullreferenceexception

Null Reference Exception Error

Hi,

I wrote two scripts to handle health in my game. One for my player to have Health and one for enemies to deal damage to my player.

In my DealDamage function it seems that Unity can't find the playerHealth.currentHealth call. It only gives back a Null Reference Exception Error.

I can not find out what I did wrong. :(

My PlayerHealth Script:

 using UnityEngine;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour 
 {
     public int startingHealth;
     public int currentHealth;
     public GameObject playerExplosion;
 
     private GameController gameController;
     private bool isDead;
 
 
     void Start()
     {
         isDead = false;
         GameObject gameControllerObject = GameObject.FindWithTag("GameController");
         if (gameControllerObject !=null)
         {
             gameController = gameControllerObject.GetComponent<GameController>();
         }
         if(gameController == null)
         {
             Debug.Log("Cannot find 'GameController' script");
         }
     }
 
 
     void Awake()
     {
         currentHealth = startingHealth;
     }
 
     public void TakeDamage(int amount)
     {
         currentHealth -= amount;
 
         if (currentHealth <= 0 && !isDead) 
         {
             Death ();
         }
 
     }
 
     void Death()
     {
         isDead = true;
         Destroy (gameObject);
         Instantiate (playerExplosion, transform.position, transform.rotation);
         gameController.GameOver();
 
     }
 }

and my EnemyHit Script:

 using UnityEngine;
 using System.Collections;
 
 public class EnemyHit : MonoBehaviour 
 {
     public int attackDamage;
 
     GameObject player;
     PlayerHealth playerHealth;
 
 
     void Start()
     {
         player = GameObject.FindGameObjectWithTag ("Player");
         playerHealth = player.GetComponent<PlayerHealth> ();
     }
 
     void OnTriggerEnter(Collider other)
     {
         if (other.gameObject == player) 
         {
             DealDamage ();
         }
     }
 
     void DealDamage()
     {
         if (playerHealth.currentHealth > 0) 
         {
             playerHealth.TakeDamage (attackDamage);
         }
     }
 }
Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Positive7 · Mar 14, 2016 at 08:54 PM 0
Share

It works fine here!

  1. Player is tagged as "Player" ?

  2. PlayerHealth script is attached to Player ?

  3. EnemyHit is attached to Enemy, Weapon or Projectile... ?

avatar image Cazo0 Positive7 · Mar 14, 2016 at 09:35 PM 0
Share
  1. Yes Player is tagged as Player.

  2. Yes it is.

  3. EnemyHit is attached to Enemy. (Enemy shall bump into my Player)

avatar image toddisarockstar · Mar 14, 2016 at 08:58 PM 0
Share

what line is the error on? that error simply means you are probobly trying to ask about an empty variable such as a gameobject variable.

avatar image Cazo0 toddisarockstar · Mar 14, 2016 at 09:36 PM 0
Share

In line 28 of the EnemyHit script where I ask wether the current health of the player is bigger than one.

1 Reply

  • Sort: 
avatar image
1

Answer by WillNode · Mar 15, 2016 at 01:45 AM

I can guess this:

  1. Your EnemyHit is disabled while the GameObject (who attaching EnemyHit), which prevent EnemyHit's Start function be called first. please note that according to the docs, OnTriggerEnter still be called even your script is disabled, so to avoid headaches, make sure you have EnemyHit enabled or....

      void DealDamage()
         {
             if(!playerHealth)
             {
                 Start();
                 if(!playerHealth)
                     Debug.LogError("PlayerHealth not found, go to solution 2");
             }
             if (playerHealth.currentHealth > 0) 
             {
                 playerHealth.TakeDamage (attackDamage);
             }
         }
    
    
  2. Make sure that ONLY your player has tag "player", unfortunely, there is no quick solution for this*, so you need to iterate/check every of gameobject in your scene whenever any other gameobject accidentally tagged as "player". or see a (not quick) solution here

PS : This can be a good Feature request :)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Cazo0 · Mar 15, 2016 at 05:05 PM 0
Share

Okay, I tried that. The debug log appeared so Unity did not find PlayerHealth, but solution 2 was negative. I didn't find any GameObject tagged as "Player" except of the Player GameObject itself.

One thing could be I've a ChildObject attached to the player which holdes the $$anonymous$$esh Filter, the $$anonymous$$esh Renderer and the $$anonymous$$esh Collider on it. This ChildObject is tagged as player too... could this cause the Error?

avatar image Cazo0 · Mar 15, 2016 at 06:50 PM 0
Share

Solved it! $$anonymous$$y problem was, that I attached the Collider of my player to the ChildObject and this Object dind't have the PlayerHealth script attached to it so there was no way for the Enemy and Player to bump into each other. The Enemy bumps into the Collider of the player's ChildObject but there is no PlayerHealth attached to it so no damage at all.

I put the collider on the Player GameObject where the PlayerHealth script is also attached to and see it works.

Thank you very much for your help! :)

Follow this Question

Answers Answers and Comments

58 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Object reference not set to an instance of an Object C# 2 Answers

I really need help with this message 0 Answers

UI null reference errors in script, but not in inspector. 1 Answer

Object reference not set to an instance of an object [HELP!] 0 Answers

PLEASE help, the Anim.SetBool or Anim.SetFloat s are sending tons of errors! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges