Are Script Instances Distinct?

If I have two sprites in a scene and both sprites use the same movement script with an Update() method, etc., it appears that there are actually two copies of the script and each sprite executes their own copy.

Essentially, each copy of the movement script acts like its own instantiated copy of the class.
Therefore, each copy of the script has its own set of private variables.

Is that truly how it works or am I deceived?

Thanks.

– Paul

P.S. I ran a test with a common script that increments and outputs a private variable counter and each copy of the script output the same number each time through the Update() method.

Yes, that’s exactly how it works.

In fact, a “class” on its own doesn’t do anything*. A class is a blueprint from which instances are created. Only instances can do things.

  • This is a simplification.

Indeed, the same code is executed in both cases but each instance works on its own independent data. It’s a good idea to become familiar with object-oriented programming.