Legacy Documentation: Version 2018.1 (Go to current version)
LanguageEnglish
  • C#
  • JS

Script language

Select your preferred scripting language. All code snippets will be displayed in this language.

InputField.OnFocus

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

protected method OnFocus(): void;
protected void OnFocus();

Description

Focus the input field initializing properties.

Handles what happens after a user selects an InputField. This is a protected property. To return the focus state use InputField.isFocused. To shift focus to another GameObject, use EventSystem.SetSelectedGameObject.

A common use of this is allowing the user to type once focussed. Another way is outputting a message when the user clicks on a field (often seen when creating passwords).

no example available in JavaScript
//Create an Input Field by going to Create>UI>Input Field. Attach this script to the Input Field GameObject

using UnityEngine; using UnityEngine.UI;

public class Example : MonoBehaviour { InputField m_InputField; void Start() { //Fetch the Input Field component from the GameObject m_InputField = GetComponent<InputField>(); }

void Update() { //Check if the Input Field is in focus and able to alter if (m_InputField.isFocused) { //Change the Color of the Input Field's Image to green m_InputField.GetComponent<Image>().color = Color.green; } } }

Did you find this page useful? Please give it a rating: