I am making a system where using the name of the first object in the list it will find the gameobject with that name/tag and show it/set it active. Here is the code for that
I have tested it and ActiveCat does return the correct value, however FirstCat always returns null. I have tags that match the names or the gameobject and the names in the list perfectly and I have check all of those. ActiveCat is a public string, and FirstCat is a public gameobject that never ends up with the object assigned to it. Is there something wrong with my use of find or gameobjects?
An example of what happens:
Lets say the first cat of the list is âCharmineâ
In general, DO NOT use Find-like or GetComponent/AddComponent-like methods unless there truly is no other way, eg, dynamic runtime discovery of arbitrary objects. These mechanisms are for extremely-advanced use ONLY.
If something is built into your scene or prefab, make a script and drag the reference(s) in. That will let you experience the highest rate of The Unity Way⢠success of accessing things in your game.
âStop playing âWhereâs GameWaldoâ and drag it in already!â
Hereâs why all this âFindâ stuff is CRAZY code:
The issue is that I cannot do that without using a bunch of if statements and clogging up the code. I need to get the first item in the list, which can be 1 of 10 items. Unless there is a simpler way to do that I will need a way to use find or something similar.
GameObject.FindGameObjectWithTag only returns active game objects. However, based on your screenshot, it appears that the game objects in question are inactive.
Are these game objects all in the same scene as the cats (which all have great names, btw)? It would be easier to reference them directly via the inspector, likely in a collection. If they all have a common component as well, better to reference them via said component.