Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Omega2D · Jul 17, 2015 at 04:00 AM · unity 5errornullreferenceexceptionstaticcoin

NullReferenceException Error how to solve?

Hello everyone.

I need your help,I don´t know why this error appers, because my game works same with the error. The error is here:

NullReferenceException: Object reference not set to an instance of an object Monetario.Update () (at Assets/Script/Botoes/Monetario.cs:18)

My code is here:

using UnityEngine; using System.Collections; using UnityEngine.UI;

public class Monetario : MonoBehaviour {

 public static int coins = 0;
 public UnityEngine.UI.Text moneytxt;
 // Use this for initialization
 void Start () {
     //Guarda o money
     coins = PlayerPrefs.GetInt("coins");
 }
 
 // Update is called once per frame
 void Update () {
     coins = PlayerPrefs.GetInt("coins");
     moneytxt.text = "Coins: " + coins.ToString();
 }

}

Thank you very much

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 sas_88 · Jul 17, 2015 at 05:20 AM 1
Share

Above script works fine,the only thing u have missed is to assign the UI text to moneytxt.

avatar image Omega2D · Jul 17, 2015 at 05:04 PM 0
Share

thank you, i don´t drag and drop the text component, because of this the error appear.

2 Replies

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

Answer by dhivakar · Jul 17, 2015 at 08:58 AM

Hi, You have the moneytext to be a public variable that holds UI.Text. Have you assigned the object for this variable in the editor.

If you are manually creating the object then check if you have instantiated the object.

Attach the process to unity and put a break point in the update and check if the money is not null. Else simply you can assign the value to the moneytext.text only if the value is not null like this

void Update () { coins = PlayerPrefs.GetInt("coins"); if(moneytxt != null) moneytxt.text = "Coins: " + coins.ToString(); }

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 Omega2D · Jul 17, 2015 at 05:07 PM 0
Share

Thanks,i did this changes in the code, now the is better. And the error it´s because i don´t drag and drop the text element to the inspector.

avatar image
2

Answer by alok-kr-029 · Jul 17, 2015 at 04:30 AM

the script seem to be find one thing I would like to correct is you can directly use

public Text moneytxt;

and you have to drag and drop the "Text " in inspector as it is not getting the text and it is giving null value .

hope this will solve your problem

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 Omega2D · Jul 17, 2015 at 05:03 PM 0
Share

Thank you very much, the problem is in the inspector i don´t drag and drop the "Text" like you said. Thanks for your help.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

My player figure falls down after load 1 Answer

Lots of "semantic issues" causing build to fail? 0 Answers

`System.IO.File' does not contain a definition for `ReadAllBytes'? 1 Answer

does the difference of Unity 5 pro and Unity(not pro) might be the reason why Im getting an error? 1 Answer

White Screen Editor 5.5.2f 2 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