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 /
avatar image
0
Question by Kinger61 · Jan 14, 2014 at 11:29 AM · c#errornullreferenceexception

NullReferenceException problem

I keep getting this NullReferenceException:Object reference not set to an instance of an object Bullet_Hit_Boss.OnTriggerEnter (UnityEngine.Collider.col) (at Assets/Scripts/Bullet_Hit_Boss.cs:16), but on the code below the actual part that is supposidly wrong is the "healthbar.CurHealth -=20;" ....Really cant work out why it wont work! Have practically same scripts in use eleswhere and they work fine! Any help would be appreciated, heres the two scripts being used...

public class Bullet_Hit_Boss : MonoBehaviour {

 void OnTriggerEnter(Collider col)
 {        
     
     if(col.gameObject.tag == "Boss") 
     {
         
         Debug.Log("Bullet Hit Boss !"); // outputs to console
         
         Boss healthbar = GetComponent<Boss>();
         healthbar.CurHealth -= 20;
         Debug.Log("Health Decreased !");
         
         Destroy(gameObject);
     }

 }

}

public class Boss : MonoBehaviour {

 public int maxHealth = 100;
 public int CurHealth = 100;
 
   
 public GUISkin HealthBar; 
  //public GUISkin HealthBarStay;
 
 public float healthBarLength;
  //public float healthBarLengthStay;
 
 
 public bool GUIdisplayed;
 
 // Use this for initialization
 void Start () 
 {
     GUIdisplayed = true;
    healthBarLength = Screen.width /3;
   //  healthBarLengthStay = Screen.width /3;
 }
 
 // Update is called once per frame
    void Update () 
 {
       AdjustCurrentHealth(0);
 }
 
 void OnGUI()
 {
  
 //    GUI.skin = HealthBarStay;
 //GUI.Box (new Rect(20,400, healthBarLengthStay, 14), curHealth + "/" + maxHealth); 
     
 if(GUIdisplayed == true)
 {
     GUI.skin = HealthBar;
 
     GUI.Box (new Rect(Screen.width/ 10 * 3.5f, Screen.height/ 10 * 6f ,     healthBarLength, 50),""); // From Right, From Top, Length, Height
 }
 
 }
 
 public void AdjustCurrentHealth(int adj)
 {
 
 CurHealth += adj;
 
    if(CurHealth <0)
     {
         CurHealth = 0;
     }
 
    if(CurHealth > maxHealth)
      CurHealth = maxHealth;
 
    if(maxHealth <1)
      maxHealth = 1;
 
 
    healthBarLength =(Screen.width /3) * (CurHealth / (float)maxHealth);
 
 }

}

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
3
Best Answer

Answer by woodoo · Jan 14, 2014 at 11:49 AM

In this line:

 Boss healthbar = GetComponent<Boss>();

You are trying to get the component Boss from your class' GameObject. The function GetComponent returns the component (if found) or null if not.

Probably it is returning null, and then the NullReferenceException is thrown when you try to update CurHealth here:

 healthbar.CurHealth -= 20;

Check if the GameObject you are trying to GetComponent<>() really has the component Boss.

Edit: The GameObject you need to get the component Boss in this case is the collider's gameObject.

To fix it, change this:

 Boss healthbar = GetComponent<Boss>();

to that:

 Boss healthbar = col.gameObject.GetComponent<Boss>();
Comment
Add comment · Show 5 · 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 Kinger61 · Jan 14, 2014 at 12:00 PM 0
Share

is the part withing the <> in my case Boss, is that not just calling the Boss script and class so that the CurHealth variable can be accessed ? sorry im kind of new to all this! It works fine in another script when i use and the HealthBar1 is only the name of the of the class and it works fine ?

avatar image woodoo · Jan 14, 2014 at 12:07 PM 0
Share

Read the edited slice of my answer above.. You were trying to get the component Boss from your current class, and not from the collided object, which in this case is the Boss itself.

avatar image Kinger61 · Jan 14, 2014 at 12:13 PM 0
Share

awh man you are a saint! didnt need the whole thing because it was bringing up an error to do with gameobject! it works perfect like this...

Boss healthbar = col.GetComponent();

wont let me put in but thats in too!

avatar image woodoo · Jan 14, 2014 at 12:21 PM 0
Share

Happy to know that it fixed your problem. Don't forget to upvote and check as selected answer if you think the answer was useful.

avatar image necrosphere999 woodoo · Feb 28, 2017 at 04:41 PM 0
Share

This answer really helped me out as well. Thank you.

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

21 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

Related Questions

Multiple Cars not working 1 Answer

Help With AI Error 2 Answers

Scripting error comes up when I press play 1 Answer

Need help calling a script to another keep getting errors 1 Answer

NullReferenceException trouble 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