Accessing Variables from another Class/Script

There are many ways to do this and highly depends on your use-case. I mainly use these three Variations...

1. Public Variable
2. Public Static Variable
3. Public getter/setter Variable

1. Public Variable - make Variable public like this:

Access public Variable from another Class:

or

2. Public Static Variable - they are easily accessible when the Class/Script
is applied to the same GameObject. Downside, it's value is not serializable 🙁

3. Public Getter/Setter Variable - So, to make this Variable serializable AND read-only to avoid potential future debug-issues, make it a private setter:

To access this Variable and read only it's value:

Close Menu