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

One script for multiple Prefabs or split?

Discussion in 'General Discussion' started by safak93, Feb 7, 2015.

?

One script for multiple Prefabs or split?

  1. One script

    50.0%
  2. Split

    50.0%
  1. safak93

    safak93

    Joined:
    Feb 23, 2014
    Posts:
    19
    I want to know from you what you prefer:
    One script with booleans(prefab1, prefab2, prefab3,...) for multiple Prefabs? Example:

    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D col)
    2.     {
    3.         if (tag == "" || col.gameObject.tag == "Ground")
    4.         {
    5.             gameObject.SetActive(false);
    6.         }
    7.         if(prefab1){
    8.             if (tag == "" || col.gameObject.tag == "Player")
    9.             {
    10.                 PlayerControl.fuel += 10;
    11.                 gameObject.SetActive(false);
    12.             }
    13.         }
    14.         if(prefab2){
    15.             if (tag == "" || col.gameObject.tag == "Player")
    16.             {
    17.                 PlayerControl.fuel += 15;
    18.                 gameObject.SetActive(false);
    19.             }
    20.         }
    21.         if(prefab3){
    22.             if (tag == "" || col.gameObject.tag == "Player")
    23.             {
    24.                 PlayerControl.fuel += 20;
    25.                 gameObject.SetActive(false);
    26.             }
    27.         }
    28.     }
    or
    Split the script for each prefab?

    What is the best choise for you and why, especially for (the best) performance on mobile devices?
     
  2. tango209

    tango209

    Joined:
    Feb 23, 2011
    Posts:
    379
    Not really enough information. Are the prefabs placed at design time or instantiated at runtime?

    I prefer to use a variable (in your example, one for incrementing the fuel variable, the 10, 15, 20 values) on the script and set it with a manager class at runtime or set it at design time if they aren't instantiated at runtime.
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Without context I can't be sure, but this code smells. There are probably some deeper structure problems going on.
     
  4. safak93

    safak93

    Joined:
    Feb 23, 2014
    Posts:
    19
    The prefabs are in a PoolManager script and instantiated at runtime. I must optimize the Pool Script so that I place the prefabs to the game and disable it without instantiating it in the Start function. I know to use variables. I also use it. This script was just a edited snipped code.


    What do you mean with structure? I'm a beginner at scripting so there might be structure problems. :)

    -

    My question was "only":
    Should I use one script for multiple prefabs or for each prefab a new script?
     
  5. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    I'm struggling to put my finger on exactly what I don't like here.

    For starters the last three cases could simply be dealt with using a float or int set via the inspector.

    As a general rule multiple bool checks one after the other could be dealt with better via another method. Especially if only one condition can be true. A switch statement, a FSM, or delegates are all methods that come to mind, as well as splitting up into multiple components.

    Then again if this is as complex as the script is going to get, then its probably fine as it is.
     
    Trexug likes this.
  6. KellyThomas

    KellyThomas

    Joined:
    Jul 1, 2012
    Posts:
    39
    If you opt for a master script that handles several prefabs with similar behavior but different values there are two broad approaches. Either use the inspector to change the values and save each as a different prefab, or programmatically change the values through code. Both have pros and cons and will suit different people and projects to different degrees.

    If opting for the programmatic approach there is a further decision to make. Either create a Factory or Manager script to instantiate and configure and instances required or store the values for each type directly in the script itself.

    As an example of the latter approach applied to the code above:
    Code (CSharp):
    1. enum PrefabType {Prefab1, Prefab2, Prefab3};
    2.  
    3. // ...
    4.  
    5.     public PrefabType prefabType;
    6.     private float fuelRate;
    7.    
    8.     public void Start() {
    9.         InitPrefabForType();
    10.     }
    11.    
    12.     private void InitPrefabForType() {
    13.         switch (prefabType) {
    14.             case PrefabType.Prefab1:
    15.                 fuelRate = 10;
    16.                 break;
    17.             case PrefabType.Prefab2:
    18.                 fuelRate = 15;
    19.                 break;
    20.             case PrefabType.Prefab3:
    21.                 fuelRate = 20;
    22.                 break;              
    23.         }
    24.     }
    25.  
    26.     void OnTriggerEnter2D(Collider2D col) {
    27.         bool deactivate = false;
    28.        
    29.         if (tag = "") {
    30.             deactivate = true;
    31.         }
    32.        
    33.         switch (col.gameObject.tag) {
    34.             case "Ground":
    35.                 deactivate = true;
    36.                 break;
    37.             case "Player":
    38.                 PlayerControl.fuel += fuelRate;
    39.                 deactivate = true;
    40.                 break;
    41.         }
    42.        
    43.         if (deactivate) {
    44.             gameObject.SetActive(false);
    45.         }
    46.     }
     
  7. Meltdown

    Meltdown

    Joined:
    Oct 13, 2010
    Posts:
    5,797
    It depends on the prefabs.

    If you have 5 types of enemies, 5 different prefabs, you would probably want each enemy inheriting from the base enemy class.

    So

    Code (CSharp):
    1. public class SwordEnemy : Enemy {}
    2.  
    3. public class ArcherEnemy : Enemy {}
    etc..
    So each prefab would have it's own script.

    But since Unity is component based, you might add something like a Buff at runtime that can boost the stats of the object it is currently placed on.

    Then in that case the script would belong to multiple prefabs.

    It all depends really how you want to structure your code and what the script does.
     
  8. safak93

    safak93

    Joined:
    Feb 23, 2014
    Posts:
    19
    I have decided to use for each prefab a new script. I will add an achievement-system soon and therefore it's better to create multiple scripts.
    Thank you all for your answers. :)
     
  9. Arowx

    Arowx

    Joined:
    Nov 12, 2009
    Posts:
    8,194
    Can't you use one script and set up multiple prefabs with different refuel rates?
     
    angrypenguin and Kiwasi like this.
  10. ippdev

    ippdev

    Joined:
    Feb 7, 2010
    Posts:
    3,789
    Use one prefab and set the variables you want to deviate or control from a manager script. The advantage is that you can store the arrays in the manager for quick lookups, have one script for all prefabs and one script to manage them all. Two scripts - one prefab.