Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Should I make all Base scripts Static?

Discussion in 'Scripting' started by DarkEcho, Dec 29, 2014.

  1. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    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.
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  3. User340

    User340

    Joined:
    Feb 28, 2007
    Posts:
    3,001
    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? :eek:
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    KelsoMRK and User340 like this.
  5. Stiffx

    Stiffx

    Joined:
    Oct 27, 2014
    Posts:
    455
    the Singleton pattern is a good way to go.
    example:
    Code (csharp):
    1.  
    2. public class PlayerController : MonoBehaviour
    3. {
    4.    public static PlayerController Instance;
    5.    public float Speed = 10f;
    6.  
    7.    void Awake()
    8.    {
    9.       Instance = this;
    10.    }
    11. }
    12.  
    13. public class Test: MonoBehaviour
    14. {
    15.    void Update()
    16.    {
    17.       Debug.Log(PlayerController .Instance.Speed);
    18.    }
    19. }
    20.  
    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.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  7. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    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