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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Can't convert type GameObject to Text...

Discussion in 'Scripting' started by Larpushka, Aug 31, 2015.

  1. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    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


    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using UnityEngine.UI;
    4.  
    5.  
    6. public class ModelProperties : MonoBehaviour {
    7.     public Text UIText;
    8.  
    9. void OnMouseDown ()
    10.     {
    11.         UIText = GameObject.FindGameObjectsWithTag("Text");
    12.         UIText.text = gameObject.GetComponent<Text>;
    13.     }
    14.  
    15. }
     
  2. Yarbius

    Yarbius

    Joined:
    Apr 29, 2014
    Posts:
    22
    FindGameObjectsWithTag() returns a collection of GameObjects. You need to iterate the collection and find the one you want then use its .Text property
     
  3. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    More accurately, you want to find the one you want to use and save its Text component.
     
  4. Larpushka

    Larpushka

    Joined:
    Jan 6, 2015
    Posts:
    214
    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'


    Code (CSharp):
    1.     UIText = GameObject.FindGameObjectWithTag("Text");
    2.         UIText.text = gameObject.GetComponent<Text>().text;
     
    enpet likes this.
  5. Schneider21

    Schneider21

    Joined:
    Feb 6, 2014
    Posts:
    3,510
    The problem is you're trying to save a GameObject into a Text typed variable with that first line.
    Code (CSharp):
    1. UIText = GameObject.FindGameObjectWithTag("Text").GetComponent<Text>();
    2. UIText.text = gameObject.GetComponent<Text>().text;
     
    nonbeing, Beatenberg and depperm like this.
  6. Mecanamaral

    Mecanamaral

    Joined:
    Sep 24, 2020
    Posts:
    7
    Object reference not set to an instance of an object
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,798
    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
     
    Schneider21 likes this.