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 /
  • Help Room /
avatar image
0
Question by martipello · Jan 17, 2016 at 06:55 PM · uitextnullreferenceexception

easy UI text question text doesnt update

hi im trying to update a text from a value from another script but nothing gets updated heres what my script is doing

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

 public class coinTextUpdate : MonoBehaviour {
 Text coinText;
 public int coinsCollected;
 coincollision coinScript;
 // Use this for initialization
 void Awake () {
     
 }
 
 // Update is called once per frame
 void Update () {
     //coinText = coinText.GetComponent<Text>();
     coinScript = gameObject.GetComponent<coincollision>();
     coinsCollected = coinScript.coinCollected;
     coinText.text = "Score: " + coinScript.coinCollected.ToString();
 }
 }


the script is attached to the text object attached to a canvas and the error i get is

 NullReferenceException: Object reference not set to an instance of an object
 coinTextUpdate.Update () (at Assets/scripts/coinTextUpdate.cs:16)


and im sure someone will say something like that error is telling you exactly what you need to know! but i just cannot see it any help please?

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

Answer by corn · Jan 17, 2016 at 07:13 PM

Alright, so let's see what's wrong here. As you know, the faulty line is :

 coinText = coinText.GetComponent<Text>();

coinText is already a Text, so there's no point in trying to get its Text component. I guess what you meant to do was to get a Text attached to the same GameObject your script is :

 coinText = GetComponent<Text>();

What happens here is that when the script hits line 16, coinText has not been initialized, it is null. So you cannot call GetComponent on coinText, hence the NullRefException.

So you just have to remove coinText. and your script will work. However, using GetComponent in Update is a bad habit, so this script should be improved a bit. Put all your GetComponent calls in Awake : you only need to get references to your other Components once, not every frame.

 void Awake () 
 {
      // Always get your references in Awake or Start, not Update
      coinText = GetComponent<Text>();     
      coinScript = GetComponent<coincollision>();
 }
  
 void Update () 
 {
      // It's good practice to check if GetComponent found what you were looking for
      if (coinText != null && coinScript != null)
      {
           coinsCollected = coinScript.coinCollected;
           coinText.text = "Score: " + coinScript.coinCollected.ToString();
      }
  }

Another way of finding these components would be to Serialize them and set them in the Inspector via drag and drop.

 public class coinTextUpdate : MonoBehaviour 
 {
     [SerializeField]
     Text coinText;
     [SerializeField]
     coincollision coinScript;
 
     ...
 }

Then you will be able to see a Text field and a coincollision field in the Inspector. Simply drag and drop the GameObjects to which the Text and coincollision are attached (in this case, the same GameObject this script is attached to), and you're good to go, and you can even remove the GetComponent calls in Awake since you won't need them anymore.

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 martipello · Jan 17, 2016 at 07:30 PM 0
Share

this is a beautiful answer i'm new to Unity but obviously i know finding an object is an intensive method and i also know that update is called every frame but didnt really put the two together and think how damaging it is so thank you very much ill also be taking your advice and serializing all the things! thanks again

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

45 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

Related Questions

UI Text rendering in front and behind planes 0 Answers

UI TEXT not showing up in game mode 1 Answer

GameObject destroyed when changing scene 1 Answer

Object reference not set to instance of an object? 1 Answer

null reference exception with text 0 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