Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
0
Question by losingisfun · Aug 27, 2016 at 02:57 PM · findfindgameobjectswithtagfind-gameobject

Cant Find.() Text component in Canvas

Heres the problem, I have a GameObject script that is trying to Find a Text component on a GameObject in the Canvas. Here is some code that i've tried, all giving null references:

     public class PlayerController : MonoBehaviour  {

 public Text coinText;

 void Start ()
 {
     coinText = GameObject.Find("Canvas/coinText").GetComponent[UnityEngine.UI.Text]();
     }

     void example ()
     {
             coinText.text = coins.ToString ();
     }

}

Ive also tried Find("Canvas/coinText").GetComponent[Text](); and Find("coinText").GetComponent[UnityEngine.UI.Text]();

No matter what i do it won't find the GameObject, it's been driving me crazy, everyone else seems to use the exact same code and it works for them? Any ideas what I could be missing?

(And yes, i'm using angle brackets not square brackets)

Comment
Add comment · Show 10
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 mj321 · Aug 27, 2016 at 03:14 PM 0
Share

First i'd separate the Find() and the GetComponent() call in order to find out which one of them fails.

Is the object active? GameObject.Find() doesn't find inactive objects.

avatar image losingisfun mj321 · Aug 27, 2016 at 03:29 PM 0
Share

Yes the GameObject is active. I'll separate the calls and see.

avatar image losingisfun losingisfun · Aug 27, 2016 at 03:54 PM 0
Share

I've done a basic check on the find() call, and it seems to be the issue. I did a simple test, created a GameObject, outside of the Canvas, without any parent or child, in the same path as the script holding object. Then did so:

public GameObject testObject;

void start() { testObject = GameObject.Find("testObject"); }

and it still returned null... I'm confused, Find() doesn't seem to work at all, is there something i'm missing about how it paths and finds game objects?

Show more comments
avatar image Dream_in_code · Aug 27, 2016 at 03:19 PM 0
Share

using UnityEngine.UI

public Text coinText;

   coinText.text = value.ToString();
avatar image losingisfun Dream_in_code · Aug 27, 2016 at 03:30 PM 0
Share

Yes, i'm using UnityEngine.UI. ToString won't work, because the variable is co$$anonymous$$g up null.

avatar image Dream_in_code losingisfun · Aug 27, 2016 at 03:32 PM 0
Share

What is the name of the text in your Inspector? I think inside gameobject.Find("") you must add the name of the text which is in your inspector not the class variable name

Show more comments
Show more comments

2 Replies

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

Answer by losingisfun · Aug 28, 2016 at 06:04 AM

I've found the problem.

So I was trying to Find() a few GameObjects, one of which was inactive, which didn't matter, I was happy to just fix it up later, but the problem was that 'that' GameObject, which was inactive, was not just returning null, like the others, it WAS finding the inactive GameObject, but instead of coming back as null, or just ignoring it, it was being considered an error in the function itself, and so everything after that line was getting dropped. So the other Find() calls worked as long as i put them before the inactive Find() call. (I've removed that line since)

Unity Scripting API says "This function only returns active game objects." Which is fine, but what it doesn't mention is that if the GameObject you are looking for is not active, it doesn't return null, it drops the rest of the function without giving an error. (Which can be problematic if someone tries to check a GameObject's active state with Find() calls)

This should be fixed, reworked, or at the very least mentioned in the documentation, it's not intuitive to know this.

On further thought, I can't see why Find() can't be used to store inactive GameObjects into variables, when you can do it manually through the Hierarchy and inspector, with no problems. And by what i've just discovered, it shows that it DOES find the game object, it just refuses to handle it.

Comment
Add comment · 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
0

Answer by Hell_Rider253 · Aug 27, 2016 at 04:15 PM

have you tried:

using UnityEngine.UI;

public class PlayerController : MonoBehaviour {

public Text coinText;

void Start(){

coinText = GameObject.Find("coinText").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 Dream_in_code · Aug 27, 2016 at 04:17 PM 0
Share

read the comments :)

avatar image losingisfun · Aug 28, 2016 at 04:01 AM 0
Share

I've created a new bunch of objects and a new dummy script to test Find(). Apparently it works very well, and when i try to attach my original script to another object, it has the same problem. It seems that the script I'm using is somehow interfering with the Find call. But not giving me any errors.

Does anyone know of any possible problems? for example, do static variables in the same script hinder Find()? or any other functions conflict with it?

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.

Follow this Question

Answers Answers and Comments

54 People are following this question.

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

Related Questions

Find All FindObjectOfType() 1 Answer

How Expensive is Find function? 3 Answers

How do I find my inactive objects and set them active again? 3 Answers

Problem in finding objects with a particular tag 0 Answers

How to reference a GameObject that shares position with gameObject 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges