C
C#4mo ago
Poseidon

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
No description
No description
No description
8 Replies
The Fog from Human Resources
why not store things like this in a game manager if many objects need to access it
Anton
Anton4mo ago
FindObjectOfType is a method, you missed ()
The Fog from Human Resources
it can get expensive if you need to re fetch the whole object every time
Anton
Anton4mo ago
Just don't use static i.e. global variables
The Fog from Human Resources
iirc theres no need for static in Unity you will have access to a game manager class (at least its like that in godot)
Poseidon
PoseidonOP4mo ago
ok thanks will try
Cattywampus
Cattywampus4mo ago
should be
var test = FindObjectOfType<CameraController>();

//OR

var test = FindObjectOfType(typeof(CameraController));
var test = FindObjectOfType<CameraController>();

//OR

var test = FindObjectOfType(typeof(CameraController));
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.
Buddy
Buddy4mo ago
Prefer generics <> as it's much faster.
Want results from more Discord servers?
Add your server