Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

ArgumentException when I try to get TextField from GameObject

Discussion in '2D' started by qyalexwen, Sep 5, 2022.

  1. qyalexwen

    qyalexwen

    Joined:
    Jul 29, 2018
    Posts:
    8
    1. I want to set the text of a "Text" or an "InputField" in the C# script.
    2. "Text" is actually "TextElement" in C#, and "InputField" is "TextField".
    These are inconsistent.

    3. I attached a script to a button, and it has a public member:
    public GameObject txt;
    I drag and drop the TextField to that component in the UI.
    4. In the script, when the button is clicked, these are executed:
    TextField t = txt.GetComponent<TextField>();
    t.value = "efghi";
    5. When it runs and I click on the button, this error occurs:
    ArgumentException: GetComponent requires that the requested component 'TextField' derives from MonoBehaviour or Component or is an interface.
    So how can I archieve #1 ?

    Thanks a lot.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    I'll bet you originally did this as an InputField or Text variable and then changed its type.

    When you change the type, you have to re-drag the object in. Try re-dragging the GameObject you expect into here.
     
  3. qyalexwen

    qyalexwen

    Joined:
    Jul 29, 2018
    Posts:
    8
    Thanks for your reply.

    You're right. I orginally set the member as TextElement. Although it's already public, I can't see the property in the component field of the thing it is attached to. So I can't do drag and drop.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    Did you fix all the errors and meet all the other criteria for seeing a field?

    Make sure your log console selector buttons are enabled. See this graphic:

    https://forum.unity.com/threads/cant-add-script-component.632746/#post-4239121

    https://forum.unity.com/threads/update-function-not-working.953477/#post-6215873

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.
     
  5. qyalexwen

    qyalexwen

    Joined:
    Jul 29, 2018
    Posts:
    8
    Thanks again for the reply.

    If I use a public member variable "TextField", there is no place I can drag and drop the TextField to.
    The whole script is here: (ChangeTexst.cs)
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UIElements;
    public class ChangeText : MonoBehaviour
    {

    public TextField txt;

    // Start is called before the first frame update
    void Start()
    {

    }

    public void OnClick()
    {
    // TextField t = txt.GetComponent<TextField>();
    txt.value = "efghi";

    }
    // Update is called once per frame
    void Update()
    {

    }
    }


    ui1.png
     
  6. qyalexwen

    qyalexwen

    Joined:
    Jul 29, 2018
    Posts:
    8
    If I use "GameObject" instead, the first error is:
    ArgumentException: GetComponent requires that the requested component 'TextField' derives from MonoBehaviour or Component or is an interface.
    UnityEngine.GameObject.GetComponent[T] () (at /home/bokken/buildslave/unity/build/Runtime/Export/Scripting/GameObject.bindings.cs:28)
    ChangeText.OnClick () (at Assets/Scenes/ChangeText.cs:20)

    Screenshot is attached.

    The whole script is:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UIElements;
    public class ChangeText : MonoBehaviour
    {

    public GameObject txt;

    // Start is called before the first frame update
    void Start()
    {

    }

    public void OnClick()
    {
    TextField t = txt.GetComponent<TextField>();
    t.value = "efghi";

    }
    // Update is called once per frame
    void Update()
    {

    }
    }

    ui2.png
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    This is your issue: extremely confusing similar package names chosen by Unity. :/

    What you want (I believe) is actually:

    Code (csharp):
    1. using UnityEngine.UI;
    That comes in a different package.
     
  8. qyalexwen

    qyalexwen

    Joined:
    Jul 29, 2018
    Posts:
    8
  9. qyalexwen

    qyalexwen

    Joined:
    Jul 29, 2018
    Posts:
    8

    This is so weird!
     
  10. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,970
    This is one of the most annoying things: UI has been plucked out of Unity into a package, so it doesn't even install by default last I checked, whereas lots of other useless (to new users) packages (like Collab and Testing) do install by default.

    And since the UI is in a new package and has new docs, the docs are completely unlinked from Unity's main documentation tree, and they are FAR worse documentation than when they were in the main doc tree, alas. No example code, poor cross links, poor visibility of the entire class and properties at once, etc.

    Since UI has been stable a while, you could always go back to when the docs for it were good:

    https://docs.unity3d.com/550/Documentation/ScriptReference/index.html