Hi Guys,
I’m planning to make a collaborative project, which will need to be extensible after release. With that I want to make a slightly more strict formula for how to contact other GameObjects in a scene. Now there’s loads of ways to do this, but I want to pick one that is the most professionally practiced, and healthy for longevity in a team.
Here’s the ways I can think of:
-
GameObject.Find() - The issue being it’s dependancy of the name of the object. Another person changes it’s name breaks the link. Plus it’s processor cost.
-
GameObject.FindByTag() - Can get messy when needed for many single instance, or multiple unique instances.
-
Interfaces - Handy, but still must use a GameObject.FindAllOfSomething to find them, and run through a call on whichever one you want, or all.
-
Public Variable - Codewise pretty dangerous, as can be changed by accident by another coder.
-
[SerialiseField] Private Variable - Closer, but means each instance must be drag and dropped in the editor. If there’s a scene that crashes and loses it’s meta for it’s references, you’d need to do it all again.
-
GameObject.SendMessage - Stops the need for .GetComponent(), which is nice, but still needs to find the GameObject first. Without direct association (collision, Physics.Raycast), there’s no healthy way.
-
Singleton with static reference - Only useful as a singleton pattern. Don’t want to over use it for anything else. For that instance it’s handy.
-
Event (Delegate) - Sounds more promising, but need to find out more.
-
Instantiate in AsyncOperation before scene load and keep reference - Handy, but will only give script it was instantiated by it’s reference. Without the New GameObject with Constructor parameters, will be difficult to create other references to it without using another above method (is that right?).
They are the ways I can think of. What’s the proper or most popular method, if there is one?
(P.S I might have a few of them slightly wrong)
Thanks!