Scripts that aren't in an object

Hi everyone, I just started learning Unity yesterday so I’m not familiar on some of the concepts.

I understand that a script can be attached on an object and activated through the events or timer. I’m trying to make a way to validate the contents of the scripts in object A and object B.

My question is, does a script need to be attached on an object in order for it to run or can it be called by other scripts and how?

Here are some details of what I’m trying to make if it’s necessary:
I’m trying to make a turn based tactics game. I want to check for character details (which character is selected, is he eligible to move/attack, his current location, etc) and which action was selected after the character was selected (attack, move, skill). I’m currently using C# since it is what I’m familiar with compared to JS and Boo.

Thanks in advance

My question is, does a script need to
be attached on an object in order for
it to run or can it be called by other
scripts and how?

If your script derives from MonoBehaviour then it must be attached to a game object. The reason you derive from MonoBehaviour in most cases is because it contains methods and properties for modifiying a game object and its components.

Non-Static

There are a few ways to write scripts which don’t need to be attached to a game object. If you create an object which doesn’t derive from MonoBehaviour then it can be instantiated with the new keyword like so:

MeaningCalculator calc = new MeaningCalculator();

MeaningCalculator would contain methods and fields which you access like so:

calc.CalculateMeaningOf("life");

This is useful when you require multiple instances of the same object. Say you want 3 different calculators you just instantiate 3 new calculators, and they exist independently of one another. However it will contain none of unity’s functionality. You’d commonly use them for storing helper methods, data, and such.

Static

In cases where you need only one of something you can create a static class. A static class is the same as a non-static class except it can’t be instanced. There is only one! This means you can access its members globally from any script.

Scriptable Object

Unity has an object called a Scriptable object which you can derive from to create objects which need not be attached to game objects. They’re useful for storing data, and can also be saved as assets.

You can have static classes. Those are classes that are not instantiated and are accessible throughout all the project, without any need to reference them. i.e.

public static MyStaticClass {

    public static void HelloWorld () {
        Debug.Log("Hello WOrld!");
    }
}

public class MyMonoBehaviourClass : MonoBehaviour {

    private void Awake () {
        MyStaticClass.HelloWorld ();
    }
}

You can also use normal classes (i.e. one that does not inherits from MonoBehaviour) and use this class’ constructor to add a reference to them in other classes.

public class MyNormalClass {
    
    public void HelloWorld () {
        Debug.Log("Hello World!");
    }  
}

public class MyMonoBehaviourClass : MonoBehaviour {

    private MyNormalClass a;

    private void Awake () {
        a = new MyNormalClass();
        a.HelloWorld ();
    }
}