Hi,
functions like GameObject.Find and FindTypeOf don’t find disabled objects.
But we need it.
We Exactly need it.
You should override your existant functions with option finddisabled object (true or false)
Already covered:
FindObjectOfType already has an overload to include inactive objects. And if you MUST use a Find method, use FindObjectOfType over GameObject.Find 100% of the time.
Through the real solution is to never use either method.
Remember the first rule of GameObject.Find():
Do not use GameObject.Find();
More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html
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 stuff is CRAZY code:
what is the right answer so. Instead of being problem focused be answer focused.
What is the substitute for Find() function.
The replacement for a Find or similar methods is always context dependant. So if you have a particular problem where you’re over-relying on GameObject.Find or FindObjectOfType, describe it, and then we can provide an answer.
In any case, you got your answer already. FindObjectOfType does have an overload that allows it find inactive objects. If you don’t, then you are on an old version of Unity that’s not getting updated anyway, so you’d be in no position to be asking for API changes.
Did you not read the link above? I’ll post it for you again.
More information: https://starmanta.gitbooks.io/unitytipsredux/content/first-question.html
It’s a long discussion so grab some coffee, slow down and enjoy yourself. This is gamedev.
GetComponent and its sibling methods have its use cases. But Find and its siblings actually never is the right way.
Markup components etc is a much more stable and maintainable approach
Instead of using find have objects register themselves (to a game object or scriptable object), or populate a list and pull from that.
Also depending on need addressables is also viable.
In a worst case scenario where you have no choice Transform.Find(). Ideally you don’t use them because even though Transform.Find() is the fastest it’s still slow.
Its not just performance, its also the fact that using the scene hierarchy for domain logic is hacky and not a stable and maintainable approach.
Similar to tags. We dont have a single tag in our entire game