Search Unity

Feedback C# One class per script or multiple?

Discussion in 'Scripting' started by MinhocaNice, Apr 6, 2021.

  1. MinhocaNice

    MinhocaNice

    Joined:
    May 3, 2020
    Posts:
    249
    This is a discussion topic, I just want to know what you guys think about placing multiple classes into one single file like
    CSscript.cs
    , if it is a good practice, if it changes the performance (I believe it does not? But I am not sure), if it is organized, etc. And in which situations do you use it. If you don't understand what I mean, here is an example:

    Using two classes into one script:
    People.cs

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. [Serializable]
    6. public class PersonOne
    7. {
    8.     public PersonTwo Listener;
    9.     public string Question = "Can you hear me?";
    10.  
    11.     public void Ask()
    12.     {
    13.         Debug.Log(Question);
    14.         Listener.Respond();
    15.     }
    16.  
    17. }
    18.  
    19. [Serializable]
    20. public class PersonTwo
    21. {
    22.     public bool CanHear;
    23.     public string Answer = "Yes!";
    24.  
    25.     public void Respond()
    26.     {
    27.         if (CanHear)
    28.         {
    29.             Debug.Log(Answer);
    30.         }
    31.     }
    32.  
    33. }
    Using two different scripts each with a class:
    PersonOne.cs

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. [Serializable]
    6. public class PersonOne
    7. {
    8.     public PersonTwo Listener;
    9.     public string Question = "Can you hear me?";
    10.  
    11.     public void Ask()
    12.     {
    13.         Debug.Log(Question);
    14.         Listener.Respond();
    15.     }
    16.  
    17. }
    PersonTwo.cs

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using UnityEngine;
    4.  
    5. [Serializable]
    6. public class PersonTwo
    7. {
    8.     public bool CanHear;
    9.     public string Answer = "Yes!";
    10.  
    11.     public void Respond()
    12.     {
    13.         if (CanHear)
    14.         {
    15.             Debug.Log(Answer);
    16.         }
    17.     }
    18.  
    19. }
    Of course the answer is unlikely to be "Never." or "Always.", so be free to explain in what situations do you think it's best and what do you personally like to do. I personally never use them as I think it's more organizing to have each script with it's own class and that is it, but I would like to hear your opinions!
     
    Last edited: Apr 6, 2021
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,911
    Not only is it bad practice, but if they are MonoBehaviours or ScriptableObjects, Unity will simply not allow you to do it. You won't be able to attach the scripts to objects.

    It can make sense in certain circumstances where the two classes are inextricably linked. For example, a Linked List implementation might put the List class and the Node class in the same file:

    Code (CSharp):
    1. public class LinkedList {
    2.   LinkedListNode head;
    3.   LinkedListNode tail;
    4.  
    5.   // etc
    6. }
    7.  
    8. public class LinkedListNode {
    9.   LinkedListNode next;
    10.   LinkedListNode prev;
    11.  
    12.   // etc
    13. }
    Another circumstance where it makes sense is when you have a type that your class needs to use to perform its own internal operations, but will never be used outside of your class. In that circumstance, it can make sense to define that type as a nested type of the class.
     
    Last edited: Apr 6, 2021
    MinhocaNice and Lekret like this.
  3. Lekret

    Lekret

    Joined:
    Sep 10, 2020
    Posts:
    358
    File name and MonoBehaviour's name should perfectly match so second MonoBehaviour wouldn't be seen by Unity, i.e. it won't work.
    Overall it's not a good practice to put any language "units" in one file, the only exception is delegates probably. Even if it's small interface with one method related to specific MonoBehaviour, I usually put it in its own file. When I want to use struct or enum only in one class I usually make it private and nested.
     
    Last edited: Apr 6, 2021
  4. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    691
    I used multiple classes within one Monobehaviour for a UI script, but it was mainly for visual separation in the Inspector, as Unity then lets you fold/unfold the properties in each class. It also made the code clearer because there were "mini-namespaces" (?) within the class. (It made the variable names longer, but categorized them nicely.) Tradeoffs I suppose. I could also probably have done it with structs.