Should I make all Base scripts Static?

Base scripts such as Character Controller, Inventory, GUI etc (Apart from scripts that use Monobehaviour)

Im thinking if I had all those Base scripts Statics it would save time instead of having to reference where the script is while instead to can just go straight to it because its THE script, the only one and isnt copied.

Generally no. Statics can cause more problems then they are worth. One way is to have a static variable that holds a reference to the instance of the class.

While this is a valid use of static, my experience has been making everything static, then going back through later and removing the static because I found I wanted more flexibility and reusability.

Nope, base scripts can NOT be static; it won’t compile. Static classes are sealed classes and can never be inherited from.

Reference the script? What does this mean? You mean writing out the class name before the method name?

Called the Singleton pattern.

How is it a valid use case when it won’t even compile? :hushed:

Some clarification of my thoughts seems to be in order.

If I’m correctly interpreting the intent of the OP he was asking about manager classes, not base classes. You are correct that a base class cannot be static. I think the OP is misusing the term base.

The singleton pattern is one example of using a static reference, but not the only one. To be a singleton you must also guarantee that only one copy of the class exists. Its entirely possible to use a static reference without implementing the full singleton pattern (see EventSystem.current for a Unity example). That said the singleton pattern would be a good solution for the OP.

2 Likes

the Singleton pattern is a good way to go.
example:

public class PlayerController : MonoBehaviour
{
   public static PlayerController Instance;
   public float Speed = 10f;

   void Awake()
   {
      Instance = this;
   }
}

public class Test: MonoBehaviour
{
   void Update()
   {
      Debug.Log(PlayerController .Instance.Speed);
   }
}

Just keep in mind that if you going to create a network based game, this could be a problem, cause you only have one instance. Other than that i don’t see it as a bad approach.

The problem is not so much networking, every client can keep it’s own ‘instance’ of a static or singleton class. The problem is general multi player, you can’t have two static PlayerController classes in the same client.

Im sorry, I didnt explain myself clearly.

By base class i meant scripts that appear once on a gameobject, for exmaple, the camera script, inventoryGUI script, the inventory script, the playerController script etc. You will not find these or not be accessed by any other gameobject in the game. Only the playergameobject would use them. Therefore my idea of making them Static comes in, because essentially, they are THE script.

By reference script, i meant, where is this script, what gameobject is it on, how shall we get there etc

Any other things i missed out?

Again sorry