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 question was closed Oct 13, 2014 at 01:06 PM by Landern for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Danao · Oct 13, 2014 at 11:22 AM · errorcomponentreferencescorenull

How do I fix this Null Reference error and update my score?

Hello, I'm trying to get my score to update when my enemy is destroyed, but I keep getting a NullReferenceException: Object reference not set to an instance of an object error. I have the following HealthScript attached to my enemy:

 public void Damage(int damageCount)//Inflicts damage
     
 {
             hp -= damageCount;
     
             if (hp <= 0) {
                 ScoreScript score = GetComponent<ScoreScript> ();
                 score.UpdateScore (5);
                 Destroy (gameObject); //destroy enemy
                 }
                     }

Then, I have the following ScoreScript attached to my player:

 public void    UpdateScore(int scoreToAdd){
                 score += scoreToAdd;
                 }

The "score" is a public static int and seems to be recognized as such. What am I doing wrong?

Comment
Add comment · Show 2
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 thef1chesser · Oct 13, 2014 at 11:49 AM 0
Share

if it's not set... try to debug if you really get the scorescript.

avatar image Danao · Oct 13, 2014 at 12:24 PM 0
Share

Thanks for the reply thef1chesser. I did a Debug.LogError and am getting the ScoreScript.

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by 767_2 · Oct 13, 2014 at 12:26 PM

is store a static int in Scorescript if it is you can do it like that

  if (hp <= 0) {
             ScoreScript.score+=5;
             Destroy (gameObject); //destroy enemy
             }

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 Danao · Oct 13, 2014 at 12:55 PM 1
Share

Thanks 767_2. I swear I tried that earlier and at first it didn't want to work. I ended up removing my scripts and re-adding them. Now it's working the way you suggested, so the correct answer award goes to you :)

avatar image 767_2 · Oct 13, 2014 at 01:01 PM 1
Share

maybe your error is in Destroy(gameObject);

avatar image
0

Answer by Quaker_SDR · Oct 13, 2014 at 12:06 PM

May be the damage fn calls after its destoryed.

Comment
Add comment · Show 1 · 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 Danao · Oct 13, 2014 at 12:55 PM 0
Share

Thanks Quaker_SDR, but the damage function is being called after destroy.

Follow this Question

Answers Answers and Comments

33 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

Related Questions

NullReferenceException - With Network PlayerSpawn 0 Answers

Null Reference Exception Error 1 Answer

NullReferenceException: object not in inspector? 0 Answers

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

referencing a component of a gameobject 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