- Home /
Putting variable into new Unity GUI InputField and then Acessing that variable
Hi everybody! I'm having problems(obviously!) with the new Unity GUI. I'm trying to make a simple calculator for calculating the surface area of a cylinder. I've got all my text boxes set up, and there is 2 Inputs I need. Radius and Height. I have an Input Field for Radius, but I need to figure out how I'm going to get the number the user Inputs into an int variable (I might get into floats later...) and then the possibility of accessing that. Something that would do this:
Int Radius void start { Radius = InputField.Text; //Or however you might do this. }
void loop { Debug.Log("The Radius is: " + Radius); }
btw, if you didn't notice I code in C#. If you have a java example to give then that is fine too.
Answer by HarshadK · Sep 30, 2014 at 07:59 AM
You get a reference to your InputField Component. Here's I'm assigning it directly to a script variable below:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class InputFieldTest : MonoBehaviour {
public InputField radiusField;
void Start () {
Debug.Log(int.Parse(radiusField.value));
}
}
You can access the text from the InputField using the value. This will be a string. You can convert it to an integer using int.Parse() or int.TryParse().