access a script generically?

Is there a way to access the script of an object without knowing the name of the script?

Some of my scripts that are attached to various objects have the same variables, and I want to access them. However on call, I’m not sure what the script name is going to be so I need something like

GameObject.GetComponent(Whatever the script is attached to this object).DoSomething ();

Just use inheritance… Create a base class that contains the common variables and the function DoSomething(). Then all the scripts in your scene can inherit from the base class and add their own custom functionality.

Or if you want to use C# it can be done using extension methods. First you need to create an extension method for Component (which all your components derive from). More info on that here: Extension Methods - C# Programming Guide | Microsoft Learn. Although if you need to add variables to component it may not work per se.

using System.Linq

GameObject.GetComponents<Component>().ToList().ForEach(x => x.DoSomething());

If these scripts share the same variables, you should probably create one base class (derived from MonoBehaviour) containing these variables, and derive all of your classes from this base class. And then you use GetComponent method with base class type.