- Home /
Check if a guiText = a number?
The game I'm creating requires the player to collect a certain amount of objects each level, I want an endgame script that basically loads the score screen. How can I do this? When the player collects the objects they are displayed in a guiText, but I can't use
var object : guiText;
function Update(){ if(object.guiText.text = 2){ Application.loadlevel(0); } }
That's probably wrong but it was just a rough idea of what I was trying, it needs me to convert the string to int but I'm not sure how?
Ah thanks, I was just thinking of adding to a separate script but it makes more sense to keep with the collection script.
Although if anyone tries this and gets an error try "LoadLevel(0)" ins$$anonymous$$d.
yes true.. also, it's "GUIText" (guiText is a keyword for finding the GUIText component on an object.. so, in your example, theoretically, it would be like this:
var object : GUIText;
function Update(){
if(object.text == "2"){
Application.LoadLevel(0); }
}
OR
function Update(){
if(guiText.text == "2"){
... etc
Answer by Seth-Bergman · Oct 15, 2012 at 01:53 AM
if you are collecting a certain number of objects, then you should be keeping track of that as an int already.. then for the GUIText you would convert the int to a string. (You would never need to go from string to int in this case.) In order to keep track of the number of items collected it would need to be stored this way anyway. Here's a similar example:
http://answers.unity3d.com/questions/331921/collestion-of-objects.html
in this case, itemsCollected would be the int you are watching:
function Update(){
if(itemsCollected >= 2){
Application.LoadLevel(0); }
}
(for example)
(if you have an existing script to collect items, post it and I'll help fix it..)
Answer by sparkzbarca · Oct 15, 2012 at 01:54 AM
you can declare a char by placing it in single quotes or a string by placing it it double quotes
be aware
char = one character a character is basically a single keystroke, any key you can press is a character. one letter, one number, or symbols like !@#$.
string = a collection of characters
'a' is a character
'1' is a character
"a" is a string
"1" is a string
"12" is a string
12 is a int
'12' is illegal, its two characters and a character is only one.
writing out the actual value is a literal string if you have say a string variable like
string my_own_string;
and its equal to only one character like
my_own_string = 'a';
and you'd like to convert it to a character tehre is a function for that
my_own_string.tochar();
hope taht helps mark as answered!