- Home /
Having a GUI text as a int
Hey, I have a GUI text that counts when I destroy an object and is displayed in the GUI text on the screen. I would like to make another script that says when the number of destroyed objects shown in the GUI text is > 0, another script will become active. I was told that I need to change the GUI text I have to a float or int, since I have only whole numbers I will need to turn it into a int.
Thank you for help:)
The text of a GUIText is a string not an integer. A string can't be greater than 0 because it is not a number. You need to cast the integer to a string befor you can use it. To check if the counter is greater than 0 simply use an if statement. Something like:
public int counter = 0;
void Update(){
guiText.text = counter.ToString();
if(counter > 0){
//Do something;
}
}
Answer by Ruthra · Feb 04, 2013 at 03:22 AM
You could do like this in c#:
float floatCount;
floatCount = int.Parse(CountGuiText.text);
Answer by xxMDAxx · Feb 04, 2013 at 03:22 AM
Can you not just have the text display a 'static var enemies: int;' then:
On your GUIText you can:
guitextObject.text = whateverscriptthestaticvarison.enemies.ToString();
and elsewhere you can have:
if (whateverscriptthestaticvarison.enemies > 0) blahblahblah
Answer by LinuxHacker · Jun 19, 2013 at 06:43 PM
try to take a transform of the gui text than just take the text value on the gui text component and do whatever you want with it
Your answer
![](https://koobas.hobune.stream/wayback/20220613092108im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
"Talent Tree" global yes/false var and int? 3 Answers
UI system multiple ints as function arguments 4 Answers
Cannot convert 'callable(int) as float' to 'float'. 1 Answer
Monodevelop is expecting int from my float array 3 Answers
Enemy Position 2 Answers