Script objects

Hello everyone, thanks for reading my question.

Imagine i have 4 balls like this oo
all of them are different objects.
How can I put on a script something that will be applied to the all of 4 balls? what is the main idea that ables me to work with all the four objects on a script?

thanks for the help.

The scripts should be simply applied to each of the gameObjects in your scene (drag onto each of your objects). If you’re wanting to interact with the objects at all the same time, you can “capture” them using GameObject.FindObjectsOfType<YourScriptName>(), which will store all the copies of your script in the scene into an array (which is ‘OfType’ YourScriptName).

As for the “Main Idea”, the closest thing I can think of is your script being static. If your script is static, it shouldn’t (and cannot be) applied to an object in your scene. There are no “instances” of that script in your scene, and it is used usually for managing, helper classes, etc.

If there’s something else you’re looking for, just leave a comment.


Note: You should probably use that GameObject.FindObjectsOfType... within another script, outside of the one you have on each of your objects. For example, you will have a script that interacts with whatever object it is on. Then, you should also have another script that is on a general object in your scene (for instance, the Main Camera, since it’s generally always in the scene). The script on your Camera should call that method in the Start() function, and assign it to an array.

I see. I’ll start thinking in that way and see if i can work it out.

Thank you very much for the very complete answer.

Good work :slight_smile: