I know of several methods, but they all have drawbacks I would like to avoid:
Is there another solution/best practice for this problem you know of?
I tried researching other ways, but did not find a completely suitable approach, as described above.
If the objects are already in the scene when the game starts, just use a List<>
, and populate it manually in the editor. Then you can run through the list to find the correct object.
If the objects are generated during gameplay, then you can assign them to a List<>
when they are created.
To access the list from any place, make it's script an instance (Singleton Class), and you can simply reference the instance from anywhere.
Check out the code in the answer given here for how to properly implement a Singleton Class.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With