Reference Game Objects in Unity

Published: 24 December 2021
on channel: Boxfriend
802
33

These are some of the best ways to reference game objects in the Unity engine. There are other methods of getting a reference such as the GameObject.Find and GameObject.FindObjectsOfType methods, but those methods are generally slow because they need to traverse the hierarchy to find the object and you don't get to choose which object it finds if you have multiple that match the parameters of the search.

My thoughts on the Singleton pattern:
It's a great design pattern and can be incredibly useful. However a lot of new game developers tend to overuse them and make everything a singleton just for easy references. This is bad practice, only use the singleton pattern when it is actually necessary, for example your UI doesn't need to be a singleton, it can subscribe to events to get updates from your player or other objects.

For more info about events, take a look at these resources:

Unity Learn: https://learn.unity.com/tutorial/even...

Jason Weimann on C# Events or UnityEvents:    • Events or UnityEvents?????????  

TIMESTAMPS
00:00 Serialized References
01:26 Singleton Pattern
03:09 Dependency Injection
05:07 Events


On this page of the site you can watch the video online Reference Game Objects in Unity with a duration of hours minute second in good quality, which was uploaded by the user Boxfriend 24 December 2021, share the link with friends and acquaintances, this video has already been watched 802 times on youtube and it was liked by 33 viewers. Enjoy your viewing!