How to find and access a TextField child

I have a button that has two transform fields (addr, port) referenced to two TextField components. I want to set the text values. However, when I try to convert them to TextField, and error occurred:
error CS0030: Cannot convert type ‘UnityEngine.Transform’ to ‘UnityEngine.UIElements.TextElement’

I tried to use addr.gameObject but the same error:

Assets/OkButtonClick.cs(13,24): error CS0030: Cannot convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.UIElements.TextElement’.

How do I resolve this?

Hello. How do you expect to recieve explicit help if dont show your code…

Your problem Its like if i do this:

Gameobject MyObject= 4;

IT will say : Cannot convert type 'int' to 'UnityEngine.GameObject'.

Because MyObject needs a GameObject variable, nor a int variable.

So, in your error, it says, you are typing a Transform component, when need to type a TextElement component.

Sorry here’s more detail. Note that

  1. I have a InputField in the UI form. I want to get and set the text value when a button clicks.
  2. I have a script attached to a button, and I referenced that field in a Transform variable “port”.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class OkButtonClick : MonoBehaviour
{
public Transform addr;
public Transform port;
// Start is called before the first frame update
void Start()
{
// The following code will give an error
// Assets/OkButtonClick.cs(15,24): error CS0030: Cannot convert type ‘UnityEngine.GameObject’ to ‘UnityEngine.UIElements.TextElement’
// TextElement t = (TextElement)addr.gameObject;

   // The following code will give an error 
   // Assets/OkButtonClick.cs(19,24): error CS0030: Cannot convert type 'UnityEngine.Transform' to 'UnityEngine.UIElements.TextElement'
   TextElement t = (TextElement)port; 

   t.text = "8088"; 
   
   //addr.text = "192.168.1.166";    
   //port.text="8088"; 
}

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

public void onClick(){
    Debug.Log("Inside onClick" ); 
}

}

  1. When I tried to convert it,
    error CS0030: Cannot convert type ‘UnityEngine.Transform’ to ‘UnityEngine.UIElements.TextElement’

My questions are:

  1. What exact type is “InputField” ?
  2. How can I reference it from a button? I want to get and set the text field of that InputField.

Thanks.

You can check out this post as it explains when getting an InputField on how to make it accept numbers/integers and function that in your code:

and another post on text elements:
https://answers.unity.com/questions/1913451/how-can-i-add-a-filter-for-names-in-my-gamehow-cou.html?childToView=1913555#answer-1913555

if you need more detail, I might be able to mock up how to do it. But I’m pretty sure both of those posts will help you get it figured out :slight_smile: