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 Ryujose · Jan 04, 2015 at 04:07 AM · uitextcanvasnullreferenceexception

UI 4.6 Text - Bad call to Text or bug?

Hello community.

I'm trying to call the text via canvas UI 4.6 to change the text and make it auto fill by code, but it doesn't Works.

The error is this

NullReferenceException: Object reference not set to an instance of an object VidaScript.UpdateVida () (at Assets/Scripts/VidaScript.cs:67) VidaScript.FixedUpdate () (at Assets/Scripts/VidaScript.cs:46)

And here's the line of code ( there's only the part of the code that it's dropping the error on the console of unity )

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public Text vidaText;
 
 
 void Start()
     {
        vidaText = GetComponent<Text>();
     }
 
 void FixedUpdate () 
     
     {
         PlayerPrefs.HasKey("lives");
         UpdateVida ();
         zeroVidas();
         
 
     }
 
     void UpdateVida ()
     {
         
 vidaText.text = "1 P - L i v e s : " + vidasActuales.ToString ();
 
        
         
     }
 
 public void zeroVidas()
     {
       
                 if (vidasActuales < 0)
                         Application.LoadLevel ("GameOver");
                         
     }
 

I'll appreciate any help, theoretically this code have to work, on UI legacy it worked.

Regards!

Comment
Add comment · Show 6
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 Ossyahn · Jan 29, 2015 at 01:20 PM 0
Share

Im having the same problem, but it works fine after throwing the exception. I debugged it and de Text refference is null when it's called (which throws the exception) but after it's not null anymore. It looks like a bug to me.

Your solution didn't work for me. If you don't get the component it sure is null then. How could work for you? You said that Text is already called but I only see one call in your code example.

avatar image Ryujose · Jan 30, 2015 at 03:03 AM 0
Share

You've a Text with a parent canvas on hierarchy. Then that text throw it on the script added on the gameObject ( inspector Window ).

Remember to call this line using UnityEngine.UI; and if it doesn't Works do a if ( Text !=null ) and write your code. If you let me to view your code I can help you more.

Regards.

avatar image Ossyahn · Jan 30, 2015 at 10:40 AM 0
Share

Ok, this is my code:

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Score$$anonymous$$anager : $$anonymous$$onoBehaviour {
 
     public static int score;
     public static int completePots;
     public static int destroyedPots;
     public static int destroyedIngr;
 
     Text text;
 
     void Awake (){
         score = 0;
         completePots = 0;
         destroyedPots = 0;
         destroyedIngr = 0;
 
         text = GetComponent<Text> ();
         print(text.text);
 
     }
 
     void Update () {
 
         text.text = "Score: " + score +
                  "\nComplete Pots: " + completePots +
                  "\nDestroyed Pots: " + destroyedPots +
                  "\nDestroyed Ingr.: " + destroyedIngr;
 
     }
 
 }

I used exactly the same pattern than the unity example project (the survival shooter, with the little guy killing zombi bunnies while asleep, phase 8, "scoring points").

It should work, but I tried what you said anyway. I made the Text public and attatched it via inspector, erasing the "GetComponent" call, like you did (if I understood correctly). But no, no luck. It still works, but with the same exception.

The curious thing, though, is that if i put that "print(text.text)" in the awake function, the exception is only called then, one time. Print() is trying to access Text, it seems null, throws the exception, but then it's not and the game runs smoothly. If I don't put it, Text is first called on Update() and throw's the exception every frame, but the game still runs fine (including the Text.text that is updated properly).

That's what's bugging me. If the system doesn't find the Text for any reason it shouldn't work, the text shouldn't update, period. But it throws the exception (actually appears as null when I debug it) and then in the next step its magically not null anymore, like if the call that throws the exception in the first place made the system to find it, and the soon it's called (with exception thrown) the soon the Text reference is filled. So when it's called in the Awake function the Update function doesn't show the exception.

I could be wrong, but somthing as poltergeist as this has to be a bug.

What do you think?

avatar image Ossyahn · Jan 30, 2015 at 12:01 PM 1
Share

Ok, this is embarrassing. I had another object with the script attatched (I don't know when or how) and without any Text. Was this object which threw the exception, and not the one at the Canvas. That's why it worked fine. You where correct.

Thank you mate ;)

avatar image Ryujose · Jan 30, 2015 at 07:42 PM 0
Share

Thats good I'm glad that I could help you!

Regards.

Show more comments

1 Reply

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

Answer by Ryujose · Jan 05, 2015 at 03:57 AM

Solved.

I deleted this line, the Text is already called, so we don't need to call the component "Text".

 vidaText = GetComponent<Text>();


Thanks to people who take a moment to read my problem.

Regards!

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

27 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

Related Questions

5.3.5 - Blurry UI text after switching canvasses - until text is refreshed by turning visibility off/on or changing resolution 4 Answers

Unity UI Text Blurriness 0 Answers

Multiple Text of canvas with One script ?? 1 Answer

How do I change scene with two ui canvas text 2 Answers

Adding a string to grid layout group 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