- Home /
Way to tell if an int has been changed
is there a way to tell if an int has changed? or a string for that matter?
basically I am using textfields to change variables but when those variables get changed I need to know so that I can change another variable to a different number
how do I tell if a variable has changed or if a textfield has been edited?
Answer by DaveA · Mar 10, 2011 at 04:19 AM
The GUI text fields should be updating as they are changed, that should be automatic, but in general, something like this:
var myInt : int = 0; private var last_myInt : int = 0;
function Update() { if (last_myInt != myInt) { DoSomething(); last_myInt = myInt; } }
The Textfields are updating fine, I am just looking to change a different variable when the user decides to change the variable in the Textfield (I have allot of Textfields and using a different variable solely for each one will get ridiculously large quickly) any simple function like textfield.haschanged? or way of doing something like that?
If you check the code on this page: http://unity3d.com/support/documentation/ScriptReference/GUI.TextField.html The example shows exactly this. The assignment occurs when the user changes the text. Otherwise, use something like what I have (above) but using a string ins$$anonymous$$d of an int.
var stringToEdit : String = "Hello World";
function OnGUI () { // $$anonymous$$ake a text field that modifies stringToEdit. stringToEdit = GUI.TextField (Rect (10, 10, 200, 20), stringToEdit, 25); }
that part is working completely fine, what I want to happen is when the user enters a variable into the text I want a CO$$anonymous$$PLETELY different variable to be changed to something CO$$anonymous$$PLETELY DIFFERENT that just changes a variable to what the user typed in
Yeah same type of thing. See what yoyo has spelled out below
Answer by yoyo · Mar 10, 2011 at 06:00 AM
I would do as DaveA suggests, though I would put the check into OnGUI where you edit the value, rather than in Update. Something like (C#, untested) ...
public string myString;
void OnGUI() { string newString = GUI.TextField(myString); if (myString != newString) { myString = newString; DoSomething(); } }
Answer by Gabriel 4 · Mar 24, 2011 at 02:15 PM
In C# you can use properties.
int myNumber = 5; // Your actual number
int MyNumber { get { return myNumber; } set { NumberChanged(myNumber, value); myNumber = value; } }
void NumberChanged(int oldValue, int newValue) { // Do stuff }
In your GUI code use MyNumber instead of myNumber. When the value is changed the NumberChanged function will be called.
Answer by BerggreenDK · Jul 29, 2011 at 07:13 PM
Have you tried this approach?
http://unity3d.com/support/documentation/ScriptReference/GUI-changed.html
Your answer
Follow this Question
Related Questions
Text fields where text will scale along with resolution. 1 Answer
GUI Text field? How do I make the text so it goes on more then one line? 1 Answer
Allow only numbers in an input field 1 Answer
GUI custom textfield cursor rendering 2 Answers
Change the color of a label font that's using an EditorStyle.textField style? 1 Answer