- Home /
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!
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.
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.
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?
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 ;)
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!
Your answer
Follow this Question
Related Questions
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