I know a static variable means that the variable is set, but I need the variable to change and still be accessible from another script.
the variable is:
var HEALTH = 0;
I know the static variable is:
static var HEALTH = 0;
I just need the variable to be accessible through another script and at the same time, I need it to be able have different values.
“I need it to be able have different values.”
No. This is the opposite of what a static variable is
In computer programming, a static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program. This is in contrast to shorter-lived automatic variables, whose storage is stack allocated and deallocated on the call stack; and in contrast to objects, whose storage is dynamically allocated and deallocated in heap memory.
Variable lifetime is contrasted with scope (where a variable can be used): "global" and "local" refer to s...
What you are probably looking for is a
public variable, Public variables can be accessed by other objects (components.)
In order to do so you need to first find the game object that has the component on it
Then get the component from it:
Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.
Then access the public using .
Static variables are not the best way of accessing variables from other scripts. Have a look at
this page from the docs.
Static variables don’t belong to any one object- this is why health isn’t a good candidate for a static var. Points might be a better example- there will only ever be one instance of it, many scripts access it, and it doesn’t really belong to any one object.