Hello i just started Unity i have question about coding
i tried this script and i couldnt figure out why
‘gameplayamager’ = gameobject.FindObjectOfType();’
he is using this line. I tried passing variable between scripts not using this line of code
and it gave me an error.
I searched what FindObjectOfType and it says it returns object of specific type in this case why do you need to return specific type of object to pass varaible to another script?
i do not understand why you get an error without
‘gameplayamager’ = gameobject.FindObjectOfType();’
this line
Also what is <> this syntax used in Unity?
‘’ for example what does this indicate?
Thank you
The <T>
parameter in any method represents the type/s it takes in. You might have seen this before when working with lists List<Vector3> positions
, dictionaries Dictionary<int, string> names
, tuples, or even methods (such as GetComponent<T>();
)
The reason this is needed, is to assign the type to a variable, that takes in that type too. A real life example would be, if I wanted an apple, there are lots of different types of fruit to choose from, but I will specify, I want the apple type. Any other type would cause an error.
This is why you use FindObjectOfType<T>()
to find the type of fruit you specifically asked for. In your case, you want to get the type GameplayManager
, and so you search the hierarchy for objects with the GameplayManager script on them, and assign that script to your variable. Hope that clarifies things. @ThankyouThankyouThankyou