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 /
avatar image
0
Question by Armadillq · Mar 28, 2018 at 11:39 PM · errorscript.error messageerror-message

Receiving error"NullReferenceException: Object reference not set to an instance of an object clicktoDestroy.OnDestroy ()"

When clicking to destroy this game object I get an error that says "NullReferenceException: Object reference not set to an instance of an object clicktoDestroy.OnDestroy ()" Thanks in advance! Also im pooling clones of the game object that get deleted

 public class clicktoDestroy : MonoBehaviour
 {
     
     public Text moneytext;
     private int moneyamount;
     static GameObject MoneyPooler;
   
     
     private void Start()
     {
         moneyamount = 0;
         MoneyPooler = GameObject.Find("MoneyPooler");
     }
     public void OnMouseDown()
     { 
         Destroy(gameObject);
     }
     public void OnDestroy()
     {
         Debug.Log("+Money!");
         MoneyPooler.GetComponent<MoneyCounter>().IncreaseMoney();
 
     }
 
 }
     

And my money Counter which doesn't get destroyed

 public class MoneyCounter : MonoBehaviour
 {
     static GameObject money;
     static Text moneytext;
     static int score;
    
     public void Start()
     {
         score = 0;
     }
     public void IncreaseMoney()
     {
         moneytext.text = "$" + (score + 1);
     }
         
     }
     
 





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 bobisgod234 · Mar 28, 2018 at 11:50 PM 0
Share

Your "moneytext" variable in your $$anonymous$$oneyCounter script is never assigned to, and thus is always null.

If the object exists in the scene, replace static with public, and assign the Text component in the inspector.

avatar image Armadillq bobisgod234 · Mar 29, 2018 at 01:01 AM 0
Share

Awesome, that worked! But now my number wont go up past 1 and because i'm pooling the money go. I destroy multiple objects. Do you know how I could keep the number increasing ins$$anonymous$$d of adding one and losing it when I destroy the object?

avatar image bobisgod234 Armadillq · Mar 29, 2018 at 01:14 AM 0
Share

You need to increase the score variable in the Increase$$anonymous$$oney() function:

      public void Increase$$anonymous$$oney()
      {
          score++;
          moneytext.text = "$" + (score);
      }
avatar image Armadillq bobisgod234 · Mar 29, 2018 at 01:08 AM 0
Share

Never$$anonymous$$d I figured it out thanks for your help!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by samvid · Mar 29, 2018 at 01:08 AM

Hi @Nokit,

I think bobisgod234 is correct. You have moneytext in your MoneyCounter class defined as static variable. It should be public in order to change its value and then drag and drop the Text component in the Inspector.

Also, from the looks of it, you have defined score as static variable as well and then instantiated as 0 so what will probably happen is everytime you will call OnDestroy in the MoneyCounter the score will be 1 and that will make moneytext always stuck at 1. I may be wrong and this is the feature you want then you can ignore this or else I suggest you make a separate class for Score variable and use the instance of that class in MoneyCounter.

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

107 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 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

im getting this error: NullReferenceException: Object reference not set to an instance of an object Item.Start () (at Assets/Scripts/Inventory System/Item.js:9) 1 Answer

Migration from Unity 5.5.0f3 to Unity 2017- error and HoloLens app not building 0 Answers

how to solve -- Object reference not set to an instance of an object -- ? 3 Answers

Weird Error after Update 4 Answers

Errors i can't understand. SetSystemInterested 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