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 /
avatar image
0
Question by sfabian98 · Dec 08, 2015 at 09:08 PM · nullreferenceexceptionnull reference exception

Null Reference

These scripts were working earlier but now it says that there is a null refference.

NullReferenceException: Object reference not set to an instance of an object PlayerHealth.AddHealth (Int32 amount) (at Assets/Scripts/Player/PlayerHealth.cs:56) PlayerAddHealth.OnTriggerEnter (UnityEngine.Collider player) (at Assets/Scripts/Player/PlayerAddHealth.cs:24)

 using UnityEngine;
 using System.Collections;
 
 public class PlayerAddHealth : MonoBehaviour
 {
     public int addHealth = 10;
     public int startingHealth = 100;
     public int currentHealth;
 
     public GameObject player;
     public PlayerHealth playerHealth;
     
     void Awake ()
     {
         player = GameObject.FindGameObjectWithTag ("Player");
         playerHealth = player.GetComponent<PlayerHealth> ();
     }
 
     void OnTriggerEnter (Collider player)
     {
         if (playerHealth.currentHealth > 0)
         {
             playerHealth.AddHealth (addHealth);
         }
     }
 }
 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 

I dont really see the problem in any of these but I'm wrong so help would be appreciated.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class PlayerHealth : MonoBehaviour
 {
     public int startingHealth = 100;
     public int currentHealth;
     public int maxHealth = 100;
     public Slider HealthSlider;
     public Image damageImage;
     public AudioClip deathClip;
     public float flashSpeed = 5f;
     public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
 
     Animator anim;
     AudioSource playerAudio;
     PlayerMovement playerMovement;
     PlayerShooting playerShooting;
     bool isDead;
     bool damaged;
 
 
     void Awake ()
     {
         anim = GetComponent <Animator> ();
         playerAudio = GetComponent <AudioSource> ();
         playerMovement = GetComponent <PlayerMovement> ();
         playerShooting = GetComponentInChildren <PlayerShooting> ();
         currentHealth = startingHealth;
     }
 
     void Start ()
     {
         currentHealth = maxHealth;
     }
 
      void Update ()
     {
         if(damaged)
         {
             damageImage.color = flashColour;
         }
         
         else
         {
             damageImage.color = Color.Lerp (damageImage.color, Color.clear, flashSpeed * Time.deltaTime);
         }
         damaged = false;
     }
 
     public void AddHealth (int amount)
     {
         currentHealth += amount;
 
         HealthSlider.value = currentHealth;
 
         if (currentHealth <= 0) 
         {
             currentHealth = 0;
         }
         
         if (currentHealth >= maxHealth)
         {
             currentHealth = maxHealth;
         }
     }
 
     public void TakeDamage (int amount)
     {
         damaged = true;
 
         currentHealth -= amount;
 
         HealthSlider.value = currentHealth;
 
         playerAudio.Play ();
         
         if (currentHealth <= 0 && !isDead)
         {
             Death ();
         }
     }
     
     public void Death ()
     {
         isDead = true;
 
         playerShooting.DisableEffects ();
 
         anim.SetTrigger ("Die");
 
         playerAudio.clip = deathClip;
         playerAudio.Play ();
 
         playerMovement.enabled = false;
         playerShooting.enabled = false;
     }
 
     public void RestartLevel ()
     {
         Application.LoadLevel (Application.loadedLevel);
     }
 }
 
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by FlyingHighUp · Dec 08, 2015 at 09:15 PM

Basically "HealthSlider is null. In PlayerHealth, line 56"

Is "HealthSlider " null/empty in the inspector for PlayerHealth?

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 sfabian98 · Dec 08, 2015 at 09:25 PM 0
Share

I'll check when I get back on the computer

avatar image sfabian98 · Dec 09, 2015 at 04:22 PM 1
Share

Yeah your right thanks! I miss the smallest things sometimes.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

31 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

Related Questions

Faulty NullReferenceException? 2 Answers

Bogus Null Reference in Build only,Bogus Phony NULL REFERENCE in BUILD ONLY (works in editor) 0 Answers

NullReference Exeption thrown even when checked against 2 Answers

Im getting NullReferenceException error 1 Answer

Using TextMeshProUGUI variable in a static function is returning a NullReferenceException 0 Answers


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