I want to set the text of a “Text” or an “InputField” in the C# script.
“Text” is actually “TextElement” in C#, and “InputField” is “TextField”.
These are inconsistent.
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.
In the script, when the button is clicked, these are executed:
TextField t = txt.GetComponent();
t.value = “efghi”;
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 ?
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.
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.
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();
txt.value = “efghi”;
}
// Update is called once per frame
void Update()
{
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();
t.value = “efghi”;
}
// Update is called once per frame
void Update()
{
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: