- Home /
Is there a tutorial for Unity 4.6 that shows how to print data from a text field when a button is clicked.
I'm working on an educational application that requires a word and definition be saved to a database, but I can't get past the part where I save the word and definition data entry when a button is pressed. I'm using Unity 4.6 and C#. Any advice is appreciated. I can't find a coherent tutorial on this anywhere.
Comment
Answer by jenci1990 · Dec 01, 2014 at 02:32 PM
This is the script:
using UnityEngine;
using UnityEngine.UI; // Import the UI System
using System.Collections;
public class Test : MonoBehaviour {
public InputField myInputField; // Attach in inspector
public ArrayList myDataBase = new ArrayList();
// The Function what you call from click (MUST BE PUBLIC)
public void PressButton () {
if (myInputField.text.Length > 0) {
myDataBase.Add(myInputField.text);
Debug.Log(myInputField.text);
}else {
Debug.Log("NoText");
}
}
}
Attach this script to Canvas or other gameObject, and assign the myInputField variable in the inspector. Select your button and assing this gameObject To OnClick() Event:
Choose from FunctionsList this script name and the PressButton() function!
and play it. If you press the button, debug the text, and add it to ArrayList.
tut3.jpg
(61.4 kB)
Your answer
![](https://koobas.hobune.stream/wayback/20220613172825im_/https://answers.unity.com/themes/thub/images/avi.jpg)