Static

I don’t fully understand statics, could anyone try to explain it to me, it’s just I don’t quite get it yet.

Static variables are non dependent on instances and keeps the same value for all scripts of the same type. For example, if you make an object with an variable and want other script to access this variable you will have to set an reference to the instance or to a prefab (that will look for an available instance). If it were static, it would keep the same value across all instances, wouldn’t need to be referenced (you could access the variable only by “nameofthescript.variable”) and wouldn’t need an instance active in the scene

So if you set a brick to red using a static method, all bricks with that script attached would become red? I’m not sure why but this is the hardest thing I’ve had to understand with any bit of coding :open_mouth:

Edit: I don’t know if I’m correct there though.

I don’t think it would work since it would have to access instances attributes that are not static (mesh renderer etc.). It is used more to counters that run Across all scripts of the same time (when one script increments it, the value will change for all others too) and for identifying which instance you want something to run (you can check if current instance = desired Instance)

One good video I’ve seen recently that explains static is this

Be warned, it is lengthy, like 24 minutes, but he uses the example of calculating the area of a circle, then adding static to the variables.

I really do recommend this video, it’s C# related, but worth watching to give you another example of how statics (and instances work).

Privacy & Terms