Changing values of static variables on an instance, overrides the value on all instances of a script

I use the data on a JSON file to instantiate objects and give them parameters like speed, I instantiate them and then put them on an Array, but when I give parameters to one instance, it override the same variable on all instances.

for (i=0;i<=input["numberRobots"].AsInt;i++){
	robots_.GetComponent(Differential).speed=input["Robots"]*["speed"].AsInt;*_

}
“input” is my JSON object, Differential is the script that all the instances have.
I print the values of speed each iteration, and i get the correct values from the JSON files, but when after that, i print all the values on the array, it’s the same on all the instances.
The value of the last instance override all the others.

Hi.

From your question, it sounds like ‘speed’ is marked as ‘static’. The definition of ‘static’ is that it is 1 value shared by all instances, so that’s the behavior you’d expect. If you want 1 value per robot, don’t mark it as static.

To be clear:

  • if speed is static there will be 1 value of speed shared by all robots
  • if speed is not static there will be 1 value of speed for each robot

As you’d expect, what your code would do is set the static speed value for each robot, but because it is shared by all of them, you’d end up just overwriting it each iteration of the loop. The end result would be the static speed value that was stored for the last robot in the list.

-Chris