Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
-1
Question by DS007 · May 15, 2013 at 09:29 PM · gameobjectnullreferenceexceptiononcollisionenterhealth-deduction

NullReferenceException

plz help me with this error! NullReferenceException: Object reference not set to an instance of an object HealthScript.OnControllerColliderHit (UnityEngine.ControllerColliderHit other) (at Assets/HealthScript.cs:51)

 using UnityEngine;
 using System.Collections;
 
 public class HealthScript : MonoBehaviour {
     
     public float maxHealth = 500.0f ;
     float health = 0.0f ;
     public float regeneration = 10.0f ;
     
     // Use this for initialization
     void Start () {
         health = maxHealth ;
         }
     
     // Update is called once per frame
     void Update () {
         if(health < maxHealth){
           
             health += regeneration * Time.deltaTime ;
         }
  
            if(health > maxHealth)
             health = maxHealth ;
     
         if(health == 0 || health < 0)
             Application.LoadLevel("zerohealth");
     }
     
     void OnGUI(){
                if(health < maxHealth){
                 Color tempColour = GUI.color ;
                   GUI.color = Color.blue;
                 
                   GUI.Box (new Rect(620,60,100,30), "Health:"+health+"/"+maxHealth);
             GUI.Label(new Rect((Screen.width / 2) - 70, (Screen.height / 2) - 20, 140, 40), "OUCH THAT HURTS");
                 GUI.color = tempColour ;
         }
         }
      
     void OnControllerColliderHit(ControllerColliderHit other){
    if(other.gameObject.CompareTag("Health")){
             
              HealthAttributes otherScript =    other.gameObject.GetComponent();
             
              ApplyDamage(otherScript.damageToInflict) ;
             
         
             
     }
     }
     void ApplyDamage(float damage){
    health -= damage ;
         
 }
 }


and this is HealthAttributes script:

 using UnityEngine;
 using System.Collections;
 
 public class HealthAttributes : MonoBehaviour {
 
     public float damageToInflict = 1.0f ;
 }
Comment
Add comment · Show 1
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 robertbu · May 15, 2013 at 09:30 PM 0
Share

Please keep your title short and descriptive.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Graham-Dunnett · May 15, 2013 at 09:47 PM

 NullReferenceException: Object reference not set to an instance of an object
 HealthScript.OnControllerColliderHit (UnityEngine.ControllerColliderHit other)
 (at Assets/HealthScript.cs:51)

There are three critical pieces of information here:

  1. `NullReferenceException` - this means that you are using a variable that has not been assigned a value, and you are using it without first checking if it's not null.

  2. `HealthScript.OnControllerColliderHit` - you have a script called HealthScript and it contains a function called OnControllerColliderHit that has the problem in it somewhere.

  3. HealthScript.cs:`51` - line 51 of the script has the problem.

Now, because your question does not have the code correctly formatted I cannot easily tell you what line 51 is. You can find out, and fix it. You are calling GetComponent() without telling the function what component you want, so that's probably your mistake

Comment
Add comment · 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
0

Answer by crogersKixeye · May 15, 2013 at 09:39 PM

Basically you have to check every variable to make sure it isnt null before you try to read it.

if (a != null) doSomething().....

i dont understand where your error is happening, but in general make sure you have given it a value, and that it hasnt gone out of scope.

Comment
Add comment · 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

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

15 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

Related Questions

OnCollisionEnter GameObject throws NullReferenceException 0 Answers

Rigidbody: NullReferenceException: Object reference not set to an instance of an object 1 Answer

OnCollisionEnter Collision not detected? 1 Answer

how to check if an game object had been collided with another game object form its left or right side? 1 Answer

How to check what is getting hit with OnCollisionEnter 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