Hey,
currently trying to get the Gameobject of the InputField which calls OnValueChanged().
public void ChangeToUpper(string key)
{
if(key.ToUpper() != key)
{
Debug.Log("NOW! Oldkey: " +key+ " ; Newkey:"+key.ToUpper());
gameObject.GetComponent<InputField>().SetTextWithoutNotify(key.ToUpper());
}
}
The Debug.Log is called correctly so the error is in the gameObject.GetComponent<InputField>().SetTextWithoutNotify(key.ToUpper());
The Script is attached to an Empty Gameobject (my Manager). Now this Manager is in the OnValueChanged() calling this function.
How can i Get the Gameobject which is calling the OnValueChanged() (here the InputField)??
You will have to attach this script to the GameObject that also has the InputField. OnValueChanged only has a string parameter, so there is no way to find out the source from that.
Another option is to attach an EventTrigger component to the InputField. By default, those events include the gameobject of origin.
However, they have their own quirky setup.
void Start()
{
var inputEvent = inputField.GetComponent<EventTrigger>();
inputEvent ??= inputField.AddComponent<EventTrigger>();
var entry = new EventTrigger.Entry();
entry.eventID = EventTriggerType.PointerClick;
#if UNITY_EDITOR
UnityEditor.Events.UnityEventTools
.AddPersistentListener(entry.callback, MyOnValueChanged);
#else
entry.callback.AddListener(MyOnValueChanged);
#endif
inputEvent .triggers.Add(entry);
}
private void MyOnValueChanged(BaseEventData ped)
{
var inputField = ped.selectedObject.GetComponent<InputField>();
...
Reddit - The heart of the internet / c# - Unity3D_2019: Adding EventTriggers at runtime - Stack Overflow