- Home /
The question is answered, right answer was accepted
Why are InputFields not getting activated?
I don't understand if it's a bug in unity or wrong implementation of code but my inputFields are not getting activated when i click on the them. It activated for a fraction of a second and goes back to another inputfield. I have tried using handler in inspector and code as well but it is behaving in the same way.
I think its something to with event listeners that I am enabling using OnEnable(). Please throw some light on this.
What exactly am I doing wrong?
Thanks in advance. This is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class TabManager : MonoBehaviour
{
public InputField name1;
public InputField Date;
public InputField Month;
public InputField Year;
public InputField Phn;
private void Update()
{
}
void OnEnable()
{
name1.onEndEdit.AddListener(delegate { endEdit1(); });
Date.onValueChanged.AddListener(delegate { valueChanged(); });
Date.onEndEdit.AddListener(delegate { endEdit2(); });
Month.onValueChanged.AddListener(delegate { valueChanged2(); });
Month.onEndEdit.AddListener(delegate { endEdit3(); });
Year.onValueChanged.AddListener(delegate { valueChanged3(); });
Year.onEndEdit.AddListener(delegate { endEdit4(); });
}
//Called when Input is submitted
private void endEdit1()
{
Date.ActivateInputField();
}
private void endEdit2()
{
Month.ActivateInputField();
}
private void endEdit3()
{
Year.ActivateInputField();
}
private void endEdit4()
{
Phn.ActivateInputField();
}
private void valueChanged()
{
if(Date.text.Length == 2)
{
Month.ActivateInputField();
}
}
private void valueChanged2()
{
if (Month.text.Length == 2)
{
Year.ActivateInputField();
}
}
private void valueChanged3()
{
if (Year.text.Length == 4)
{
Phn.ActivateInputField();
}
}
void OnDisable()
{
name1.onEndEdit.RemoveAllListeners();
Debug.Log("name");
Date.onEndEdit.RemoveAllListeners();
Date.onValueChanged.RemoveAllListeners();
Debug.Log("Date");
Month.onEndEdit.RemoveAllListeners();
Month.onValueChanged.RemoveAllListeners();
Debug.Log("Month");
Year.onEndEdit.RemoveAllListeners();
Year.onValueChanged.RemoveAllListeners();
Debug.Log("Year");
}
}
I am using Unity 2018.4.9 with VS2017
Answer by HappiiGamer · Oct 15, 2019 at 07:23 PM
When you click an input field, you are selecting it, but at the same time you are calling OnEndEdit Listener on another field that was previously selected, which in turn selects another as per your code.
@HappiiGamer is it possible to have multiple on enable() functions for each input field in a single script??
If it's possible how do we reference onenable() function for individual input field in a single script? Any ideas?
OR
Do I create a condition using eventsystem to detect the name of selected field and enable respective listener for each in the same onenable() function??
Could you tell me what you are trying to achieve? OnEnable is a method that is called in every object that inherits the $$anonymous$$onoBehaviour class, and it is automatically called whenever the GameObject becomes active in the scene.
@HappiiGamer in my scene when the app starts, there are input fields for name date month year and phnum, I am trying to switch to date when editing on name ends, then switch to month field if date's text length is ==2 or editing ends, similarly for month and year, then after year field done editing in should switch to phnum field. The above code definitely switches the field but the problem is if I select name after entering value in all fields it goes to name then activates year ins$$anonymous$$d of staying on name. It is as if listeners are switching because value changed conditions for the rest of the fields are true. It should not switch field on value changed if I simply select the field after entering all values.
Follow this Question
Related Questions
Mouse drag horizontally and vertically is not working..? 0 Answers
Press button once to enter vehicle and once again to exit 1 Answer
How to rotate with mouse scroll wheel and new input system? 1 Answer
Trying to change character skins at the push of a button. C# 1 Answer
Argument 1: cannot convert from 'method group' to 'UnityAction' 1 Answer