Passing one int from one script to another
So this isa stupid Question but im using unity for a game and i want to pass one int from a script that counts how many items were collected and i want to use it in another Script as a score. I tried a lot but nothing works. I tried FindObjectOfType and it gives me an error
8 Replies
why not store things like this in a game manager if many objects need to access it
FindObjectOfType
is a method, you missed ()
it can get expensive if you need to re fetch the whole object every time
Just don't use
static
i.e. global variablesiirc theres no need for static in Unity you will have access to a game manager class (at least its like that in godot)
ok thanks will try
should be
https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
see the declaration parts. You must match one of the signatures
Not true, no difference for statics in Unity. You can use it like you would in plain c#
use
FindFirstObjectOfType
if you can and if there's only 1 instance of it as it completely removes the sorting and should be faster that the findObjectOfType.Prefer generics <> as it's much faster.