2

So, I am new to unity and I am trying to follow a tutorial, In the tutorial, they are trying to get the text of an input field however when I try it I get the error:

'GameObject' does not contain a definition for 'text' and no accessible extension method 'text' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

My code looks like this:

[SerializeField] private GameObject UsernameInput;
...
Debug.Log(UsernameInput.text);

I have added my UsernameInput like so: MyImage

I have looked at this but it doesn't help.

Any help would be greatly appreciated.

michael
  • 27
  • 1
  • 5

1 Answers1

4

The gameObject does not contain a field named 'text'. Where you want to access is to the field 'text' of the Component of type InputField.

So, in your code, you should call it as following:

    Debug.Log(UsernameInput.GetComponent<InputField>().text);

I let you here a hyperlink to the documentation about Components in Unity which talks about what they are.