In this video we see how to FIND ANY COMPONENT present in a scene through code in Unity, for example an AudioSource component, Collider, MeshRenderer or a Script that we have created and assigned to a GameObject. This allows us to find the reference of that component and thus be able to read its variables and execute its public functions, for example to access the GameObject to which that component is assigned.
With this method we can find a component of the scene at runtime (i.e. while the game or application is running) and it requires that there is ONLY ONE INSTANCE OF THE COMPONENT we want to find, because if there are two or more components of the same type in the scene, there is no guarantee that the component we find is exactly the one we are looking for.
The instruction to find a component of a specific type in Unity is "FindObjectOfType", which should not be confused with "FindObjectsOfType", this is another function that finds all components of the type indicated.
To indicate the type of component to search in the scene we use the signs "less than" and "greater than", which I cannot write in this description, see the video to know how to indicate the type of component to search.
It is not recommended to use this method in an update function such as Update or FixedUpdate because finding a SCENE COMPONENT in Unity requires, in the worst case, analyzing all the objects in the hierarchy and extracting each of their components until you find the component you are looking for.
INTRODUCTION VIDEO OF THE REFERENCE SERIES IN UNITY:
• Video
ARTICLE ON HOW TO FIND A SPEECIFIC COMPONENT FROM THE SCENE IN UNITY:
https://gamedevtraum.com/en/game-and-...
____________________________________________________________
LINKS
Portfolio: https://gamedevtraum.com/en/portfolio...
LinkedIn: / gamedevtraum
Downloads: https://gamedevtraum.itch.io/
Contact: / gamedevtraum
____________________________________________________________