how to call a variables value from another script without making it a static variable?

not even really sure where to start looking in the documentation for this, i was told earlier in the forums here that i didnt need to use a static variable to get the value from another script. it is telling me in my unity program though that i can not access public variables from another script unless they are static. I have a situation in my program where i can’t make the value static because i have it on many buttons. I then used the editor to manually set the values for each button using the same script. When it comes to saving those values sepratly though its causing issues (not letting me access that variable because its not static etc) any ideas how i can do this without making a static variable? thanks

best to post code and errors.

To set variable
GetComponent().public_variable=0;

ah, okay thanks! that is probably all i was looking for, if there is more issues ill post a code snipit. thanks! ill check into it :slight_smile:

just trying to find a way to save things using playerprefs for now. before i release the project to anyone to play ill go through and serialize the save info. Just using the playerprefs saves now to test and balance the project.

Basically, static variables are accessed through the script itself, while normal public variables are attached to instances of the script (ie. a reference that is active in the scene) and what GetComponent() is doing is getting you an instance of the script. You may want to look up some basic information about object-oriented programming.

2 Likes