I have prefabs splawning through time. Each prefab has attached script.
In this script i have:
public bool isAffected;
I want this var to be common for all instances. I know I can use some other script attached to the unique object in the scene to store “shared” vars, but I feel there’s some easier way to make the variable global\common\shared for all prefabs with the same script attached.
Do you mean that you want the value of isAffected to be synchronized between all instances of your prefabs?
I would create a class like MyPrefabGroup that contains the isAffected variable. When you instantiate the objects, they get added to the group, and can access the group’s properties. If you change isAffected on the group, all the objects will read the new value.
Thousands of times in what respect? Thousands of variables, or thousands of calls to one variable? Or thousands of references to the variable in your code?
This isn’t explicitly true in C#, but a reference is effectively just a piece of data that points to another memory address to read from. It doesn’t matter how many times that piece of data shows up in the RAM. For your purposes there is no difference from where in the RAM you request that memory read vs where the data actually is in RAM.