- Home /
Text object can't be searched for with GameObject.Find
Using Unity 5.1. I am creating a network (multiplayer) instantiated player object that has four cameras the player can choose from (forward, back, left, right). I also have a GUI HUD with a Text object names "Camera Label" for displaying the name of the currently active camera. The problem is getting a reference to Camera Label from the script that controls the camera switching (attached to the player prefab). For instance, if I do the following . . .
Text cameraLabel = GameObject.Find ("Canvas/Camera Label");
. . . I get the following error:
Assets/Scripts/CameraControl.cs(22,25): error CS0029: Cannot implicitly convert type `UnityEngine.GameObject' to `UnityEngine.UI.Text'
Looks like UI objects don't inherit from GameObject. Any suggestions here? How can I get a reference to a Text object at runtime? (Yes, I know that in normal circumstances, I could make the Text variable public and then link it to Camera Label in the inspector. But remember, this is a network-instantiated prefab, so the link needs to happen in the Start() function. I can't rely on public variables.)
Thanks for any help.
Answer by MrMeows · Oct 25, 2015 at 03:48 AM
That error will occur with all components, not just UI.Text. You need to use GetComponent.
Text cameraLabel = GameObject.Find ("Canvas/Camera Label").GetComponent[UnityEngine.UI.Text]();
Replace those square brackets with angle brackets, and it will work fine.
Are there supposed to be angle brackets in that comment? If so, I do not see them.
Damn they showed for a while and...left...Logically, those that cannot be written are showing in code section. At least they used to before the move to the new Unity Answers.
For performance and readability reasons, if a type is being used more than a reasonable amount in the script, then it would be a good idea to add it's namespace at the top of the script like using blah.blah;
Answer by OncaLupe · Oct 25, 2015 at 04:51 PM
GameObject.Find returns the object, but you're trying to get a specific component on that object. Try this:
Text cameraLabel = GameObject.Find("Canvas/Camera Label").GetComponent<Text>();
There is no component called "Text." You have to use "UnityEngine.UI.Text."
Answer by theOtherHolmes · Oct 26, 2015 at 12:02 PM
@MrMeows - your solution worked (with angle brackets, of course). Thanks.
(BTW, @OncaLupe and @fafase - I had UnityEngine.UI in my "using" statements, but your suggestion still produced this error: " Assets/Scripts/CameraControl.cs(23,25): error CS0428: Cannot convert method group GetComponent' to non-delegate type
UnityEngine.UI.Text'. Consider using parentheses to invoke the method.")
$$anonymous$$ake sure you have the () after the angle brackets on the GetComponent, since it's a method you're calling they are needed.
Your answer
Follow this Question
Related Questions
Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 Answers
Problem in text 3 Answers
Canvas Text gets garbled 0 Answers
New Unity UI with OnTriggerStay. 0 Answers
Nested Canvas - Text not PixelPerfect 2 Answers