- Home /
Get Value/String From GUIText
This is a really simple question but I cannot find an answer. I want to get a int value from a GUIText object and add a number onto it. There are three scripts all modifying a value so I want to get the current set value of the GUIText and then set the value to a variable. Here is some code...
var GUITextVar : GUIText;
var intvar : int = 0;
var currentval : int;
var equal : int;
function Update(){
intvar+=1;
//Some Code to get the current value of the GUIText
equal=currentval+intvar;
GUITextVar.text=equal;
}
The Code isn't perfect as I just made it up. It would work otherwise.
Thanks
[EDIT]The values being edited from the other scripts are not static [/EDIT]
Answer by LucasMars · Aug 01, 2013 at 09:25 PM
Here is the finished code...
var intVar : int= 0;
var equal : int;
var intValueString;
var GUITextvar : GUIText;
var currentVal : int;
var guiTextString = "0";
function OnMouseOver(){
var score = 0;
if(Input.GetMouseButtonDown(0)){
intVar+=5;
currentVal += intVar;
guiTextString=GUITextvar.text;
equal=currentVal+ int.Parse(guiTextString);
GUITextvar.text=" "+equal;
}
}
function Update(){
var currentVal : int;
}
Thanks to Jamora and RyanZimmerman87 for providing great answers and both answers have been voted up (by me, :D)
Answer by RyanZimmerman87 · Aug 01, 2013 at 09:00 PM
I haven't used GUIText for a long time so I'm not really 100% sure on this. But I would create a public static string to store the value since it is shared by multiple scripts.
int intVar = 0;
int currentVal;
int equal;
string intValueString;
void Update()
{
++intVar;
equal = currentVal + intVar;
intValueString = "" + equal;
}
Then use the intValueString for your text, but that's the part I'm not 100% on because I don't think I've used GUIText lately, should be quite straightforward though I would imagine.
Edit:
If you don't need to use the GUItext object you could just use OnGUI()
example:
public GUIStyle GUIStyleTest;
void OnGUI()
{
GUI.Label (new Rect (640 ,0, 100, 100), intValueString, GUIStyleTest);
}
Thanks for the answer. I was using Unity-Script (I think that's JavaScript) so I had to convert it. Here is the code now...
var intVar : int= 0;
var equal : int;
var intValueString;
var GUITextvar : GUIText;
var currentVal : int;
function On$$anonymous$$ouseOver(){
var score = 0;
if(Input.Get$$anonymous$$ouseButtonDown(0)){
intVar+=5;
equal = currentVal + intVar;
intValueString = equal.ToString();
GUITextvar.text = intValueString;
}
}
function Update(){
var currentVal : int;
}
The value is not static as it changes it in certain frames.
What should I do for that?
[EDIT] I've voted on your answer[/EDIT]
If it's the right answer the you should select it as the correct answer by pressing the tick mark.
I am not getting the required function from the answers but I think I know what to do now with both the answers :D
When dealing with public static variables you need to be very careful to make sure you don't mess things up if you have multiple scripts accessing it.
Just because a value is static it does not mean it can't be changed. A public static variable can be easily changed by any scripts but there is only one instance of that variable in the entire program.
So for example you could use a public static variable for the player's health because there is just one player.
But you wouldn't want to use a public static variable for the enemies health because than all enemies would have the same health ins$$anonymous$$d of individual values.
I use them a ton though in my current project and find them an extremely fast way to do certain things.
Ya sorry my code is in C# I don't know JavaScript well enough to post my answers in it in any reasonable amount of time.
But if you want a public static variable you can do something like this:
//ScriptExample1 contains the public static int
public static int equal;
//all your code to get the value of equal for this script
//SEPARATE SCRIPT ScriptExample2 also uses the public static variable.
//Example:
int newInt;
void Update()
{
newInt = ScriptExample1.equal + //anything else you might want to do with it
}
Answer by Jamora · Aug 01, 2013 at 09:11 PM
I see two choises:
If your GUIText string is actually just an int, you can use int.Parse(GUITextVar.text) to get the int value.
If there is more than just a number, say "13 apples" you would do better to store the int separately from the string and then do a string composition when you want to display it.
I'm thinking:
var GUITextVar : GUIText;
var intvar : int = 0;
var currentval : int; //this is where the current value is stored
car guiTextString : String = "Apples";
function Update(){
intvar+=1;
currentval += intvar;
GUITextVar.text=currentval.ToString() + " " + guiTextString;
}