Even after connecting the game object itself, InputField, it still doesn’t work, anyone got a fix?
Now this is a monoscript.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ButtonController : MonoBehaviour
{
public GameObject theText;
public void ClearText()
{
theText.GetComponent<InputField>().text = "";
}
}
you never assigned the theText reference in the inspector
The object you assigned doesn’t have an InputField component on it.
To eliminate the possibility of the second one and simplify your code you should just directly reference the InputField component, instead of going through the GameObject for no good reason:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ButtonController : MonoBehaviour
{
public InputField theText;
public void ClearText()
{
theText.text = "";
}
}
Then go and double check that the object is assigned properly in the inspector
Most likely this reveals the entirety of the problem at hand. You’re probably using TextMeshPro - InputField, not the legacy InputField which would be this in code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using TMPro;
public class ButtonController : MonoBehaviour
{
public TMP_InputField theText;
public void ClearText()
{
theText.text = "";
}
}
This is part of the benefit of the direct reference - it won’t even let you assign it if you’re using the wrong field type as you have been.