• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by theOtherHolmes · Oct 25, 2015 at 12:28 AM · guinetworkingtextcanvas

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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.
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Oct 25, 2015 at 06:50 AM 0
Share

the

are showing fine in code section why using []?

avatar image MrMeows fafase · Oct 25, 2015 at 04:44 PM 1
Share

Are there supposed to be angle brackets in that comment? If so, I do not see them.

avatar image fafase MrMeows · Oct 25, 2015 at 05:21 PM 0
Share

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.

avatar image starikcetin · Oct 26, 2015 at 05:25 PM 0
Share

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;

avatar image
3

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>();
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrMeows · Oct 25, 2015 at 05:10 PM 0
Share

There is no component called "Text." You have to use "UnityEngine.UI.Text."

avatar image fafase · Oct 25, 2015 at 05:22 PM 1
Share

You can add at the top:

 using UnityEngine.UI;
avatar image
0

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.")

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image OncaLupe · Oct 26, 2015 at 04:36 PM 0
Share

Make sure you have the () after the angle brackets on the GetComponent, since it's a method you're calling they are needed.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Canvas Text gets garbled 0 Answers

New Unity UI with OnTriggerStay. 0 Answers

Nested Canvas - Text not PixelPerfect 2 Answers

Unity 4.6 adding a Canvas Text onto a gameobject prefab?? -1 Answers

UI Text Component being reset on player when built 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges