Text field for numbers ONLY!

I want to make a TextField (or anything simmilar) where can I write Numbers only, I need it for in-game adjusting position so STRING in textfield supposed to be FLOAT

let’s say we have this (from help file)

sing UnityEngine;
using System.Collections;

public class example : MonoBehaviour {
public string stringToEdit = "6";
void OnGUI() {
stringToEdit = GUI.TextField(new Rect(10, 10, 200, 20), stringToEdit, 25);
}
}

so stringToEdit is string, and it supposed to be number only, but I need string for textfield, so how to convert string to float or how to make that so you can write only numbers in the textfield?

–Eric

1 Like

Every time the string is changed, check to see if it a valid float (tryparse). If it’s not - revert to the old string.

hm I’ve looked that link, I get it what it’s there. But after all that I get

string a = “12341047120”;

so now how to use that string as int or float
eg.

float b = a;

I know there is Integer/Float to String function but is there String to Integer/Float?
or can I use numeric strings without conversation? I think no, but I’ll try it now

Note: untested…

float thenumbers;
float thestring;

float.TryParse(thestring, out thenumbers);

I have used RegEx and it’s nice thing, it does what I want, only thing is now to convert number from string into the float

Alienchild I don’t get your code at all, I have copied it but it is not good at all, I’m writing in C#, “TryParse” isn’t in unity’s help file so I don’t know how to use it and I have STRING and FLOAT variable.

now this is the code for what do I need this:

Selectionobjpositionx = GUI.TextField (new Rect (Screen.width*4/5+35, 100, Screen.width/5-40, 25), Selectionobjpositionx, 25);
Selectionobjpositionx = Regex.Replace(Selectionobjpositionx, @"[^0-9.]", "");
if (Selectionobj.tag != "EditorPlane") Selectionobj.transform.position.x = Selectionobjpositionx;

Selectionobj is gameobject variable
Selectionobjpositionx is string variable it shows x position of Selectionobj

now when I change number in TextField I want to change x position of Selectionobj

Your using C#… take a look at the C# docs:

http://msdn.microsoft.com/en-us/library/0xh24xh4.aspx

I think it might be a good idea for you to start doing some C# tuts to get a better grasp of the language and the .net framework.

y I’m learning C# by myself also I understand this code on the link you gave me but I don’t know how to convert it for unity…

i have tryed to do it:

GUI.Label(new Rect(Screen.width*4/5+5, 100, 25, 25), "x:");
Selectionobjpositionx = GUI.TextField (new Rect (Screen.width*4/5+35, 100, Screen.width/5-40, 25), Selectionobjpositionx, 25);
Selectionobjpositionx = Regex.Replace(Selectionobjpositionx, @"[^0-9.]", "");
[COLOR="red"]float result;
style stil = System.Globalization.NumberStyles.AllowDecimalPoint;
culture kultura = System.Globalization.CultureInfo.CreateSpecificCulture("en-GB");

TryParse(Selectionobjpositionx, stil, kultura, out result);
if (Selectionobj.tag != "EditorPlane") Selectionobj.transform.position.x = result;[/COLOR]

I don’t know how to do it. I understand to the regex, other things were more like experimenting…
but ok I’ll do it on some other way like increase and decrease buttons

ty anyway :slight_smile: