简体   繁体   中英

How to reference an InputField in Unity 5 using C#

I want to reference an InputField within Unity 5 using C#, but cannot figure out how to.

My goal is to take the text from the InputField when a button is clicked and use that text as a variable within other parts of the project.

I've tried using GameObject.Find("IPInput").GetComponent<Text>(); but that doesn't seem to work. I am using UnityEngine.UI so it's not that.

I think you're confusing the static Text component with InputField . Try this:

InputField field = GameObject.Find("IPInput").GetComponent<InputField>();
Debug.Log(field.text);

Side note, it's not very efficient to query by GameObject name so depending on what you're doing, you might want to just add this field to your component handling the button click:

public InputField field;

Then drag the input field into there in the inspector, and you won't have to call GameObject.Find() or GetComponent() . Much better than hard-coded object names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM