- Home /
Find GameObject in Canvas
Hi all. I have one problem with my script in C#. I try to find one Game object that is in one Canvas. But the script not found.
I try with:
GameObject.Find("Star_1").SetActive(true);
OR
GameObject.FindGameObjectWithTag("Star_1").SetActive(true);
if I put it out of the canvas work. How can i do?
Is the game object inactive in the hierarchy when placed under the canvas? GameObject.Find will only find active objects. Otherwise double check the exact name and make sure the tag is applied.
Answer by Huacanacha · Mar 23, 2015 at 08:58 PM
Per your reply the problem is that the gameobject is inactive. Find only returns active objects.
There are two ways to find inactive objects that I know of:
Resources.FindObjectsOfTypeAll - you can search by script (better) or by GameObject then find the object you want in the returned list. SLOW.
Manually searching the transform tree. In your case you could start at the canvas object. Can be slow.
However, I suggest you rethink your approach to the problem. If the object exists in the scene hierarchy at design time (i.e. not added via Instantiate or streamed assets) then just setup a reference to it in the inspector. Have a public field in the script that needs to access the script you are trying to find, then assign it in the editor. This will also avoid any performance hit.
Answer by Pharaoh_ · Mar 23, 2015 at 07:27 PM
What if you derive it from the Canvas? Canvas.FindObjectOfType?
Your answer
![](https://koobas.hobune.stream/wayback/20220613191035im_/https://answers.unity.com/themes/thub/images/avi.jpg)