- Home /
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)
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.
Yes the GameObject is active. I'll separate the calls and see.
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?
using UnityEngine.UI
public Text coinText;
coinText.text = value.ToString();
Yes, i'm using UnityEngine.UI. ToString won't work, because the variable is co$$anonymous$$g up null.
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
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.
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]();
}
}
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?