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. Dismiss Notice

Cannot implicitly converter type to UnityEngine.UI.Text

Discussion in 'Scripting' started by Darkmyst, Feb 14, 2015.

  1. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    So i knew i would get this error when i tried to find the gameobject but what else do i do?
    it doesn't seem like i can put anything after i find the object, like if i wanted the transform i would do GameObject.Find("gun").transform.

    Ill include a snippet of my code:

    1. orbHUD = GameObject.Find ("orbs Value");
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    GameObject.Find("name").GetComponent<UnityEngine.UI.text>()
     
    Darkmyst likes this.
  3. Darkmyst

    Darkmyst

    Joined:
    Aug 3, 2013
    Posts:
    35
    Just incase anyone see this, it should be UnityEngine.UI.Text

    So:

    1. GameObject.Find("name").GetComponent<UnityEngine.UI.Text>()

    :)
     
  4. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Or, if you're using Text (or other UI components) multiple times in a script you can add the UnityEngine.UI part to the top instead of in every call.
    Code (csharp):
    1.  
    2. using UnityEngine.UI;
    3.  
    4. void Awake () {
    5.     GameObject.Find ("name").GetComponent<Text> ();
    6. }
    7.  
     
  5. kdubnz

    kdubnz

    Joined:
    Apr 19, 2014
    Posts:
    177
    Shouldn't the result of that statement be assigned to a variable so it can be used ??
     
  6. DanielQuick

    DanielQuick

    Joined:
    Dec 31, 2010
    Posts:
    3,137
    Sure, it was just an example of not having to write UnityEngine.UI.Text every time. What I wrote doesn't do anything.