- Home /
 
Displaying variable on UI text every frame (JS)
Hi all,
I simply want to have a counter for a variable which increases each frame.
I can't figure out why this code won't work: (JS)
  var oofcounter : UnityEngine.UI.Text;
  
  function Update () {
  oofcounter.text = "OOFs: " + oofs.ToString();
  }
 
               "oofs" is the variable that changes every frame.
"oofcounter" is the name of the UI text as a game object.
Answer by Bip901 · Aug 18, 2017 at 01:17 PM
You didn't really tell us what you mean by "won't work"... [Do you get an error? Is the text not changing? Is the text not showing up?] but here are some possible fixes:
Make sure you have this line in the top of your code:
 import UnityEngine.UI;
 
               You also don't need to write UnityEngine.UI.Text, just use the type "Text":
 var oofcounter : Text;
 
               Don't forget to drag the GameObject "oofcounter" into the slot of the variable "oofcounter" in the inspector, as well. You have to reference it.
Thank you for the help, works like a dream. Sorry for not explaining clearly, what I meant was that the text wasn't even appearing, don't worry though it's working great now. Thanks again, I appreciate it.
Your answer
 
             Follow this Question
Related Questions
Variable won't appear on UI text (JS) 0 Answers
Adding a point to a variable is making it not show. 0 Answers
Add multiple ints to a string line. 2 Answers
Text UI is not being assigned 2 Answers
I can't print a int.ToString() variable in a guiText.text 1 Answer