Can't convert type GameObject to Text...

Hey guys. So I have a game object with a text component, and I have UI Text canvas on the hierarchy. I want that every time an object is clicked, the text component on the game object will be copied to the UI Text on the canvas. I’m getting two errors:

Assets/Scripts/ModelProperties.cs(14,17): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[ ]' to UnityEngine.UI.Text’

Assets/Scripts/ModelProperties.cs(15,24): error CS0428: Cannot convert method group GetComponent' to non-delegate type string’. Consider using parentheses to invoke the method

using UnityEngine;
using System.Collections;
using UnityEngine.UI;


public class ModelProperties : MonoBehaviour {
    public Text UIText;

void OnMouseDown ()
    {
        UIText = GameObject.FindGameObjectsWithTag("Text");
        UIText.text = gameObject.GetComponent<Text>;
    }

}

FindGameObjectsWithTag() returns a collection of GameObjects. You need to iterate the collection and find the one you want then use its .Text property

More accurately, you want to find the one you want to use and save its Text component.

Yea I accidentally used the plural version, but the error still remains.

Assets/Scripts/ModelProperties.cs(14,17): error CS0029: Cannot implicitly convert type UnityEngine.GameObject' to UnityEngine.UI.Text’

    UIText = GameObject.FindGameObjectWithTag("Text");
        UIText.text = gameObject.GetComponent<Text>().text;
1 Like

The problem is you’re trying to save a GameObject into a Text typed variable with that first line.

UIText = GameObject.FindGameObjectWithTag("Text").GetComponent<Text>();
UIText.text = gameObject.GetComponent<Text>().text;
3 Likes

Object reference not set to an instance of an object

Don’t reply to a five year old post with an incomplete and completely unrelated error.

Make your own post. It’s free.

How to report problems productively in the Unity3D forums:

http://plbm.com/?p=220

1 Like