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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much, this helped me greatly and saved me massive amounts of time. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.