Search Unity

Having multiple components of the same type on the same GameObject?

Discussion in 'General Discussion' started by ryanawe, Oct 17, 2019.

  1. ryanawe

    ryanawe

    Joined:
    Jul 1, 2015
    Posts:
    34
    What are some of the best examples (of usefulness) of having multiple components of the same type on the same GameObject?

    "Why would anyone add multiple components of the same type on the same GameObject?"
    What's the best answer to that question?

    I'm looking for as many excellent examples as possible.

    Thank you!
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Often you can add status effects by simply adding new components to a GameObject.

    Its not unusual to have multiple joints of the same type on a GameObject.

    Plenty of use cases for multiple colliders on the same GameObject.
     
    ryanawe likes this.
  3. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    9,052
    Sound fx, visual effect triggers. Components that have multiple actions, etc.
     
    ryanawe likes this.
  4. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,780
    Not direct answer regarding multiple components, but you can have component with an array/list, which can be treated kind of like multiple same components.
     
    Last edited: Oct 17, 2019
    ryanawe likes this.
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Just depends on what you want to do.
    There's no overall upside or downside to it.
     
    ryanawe likes this.
  6. Murgilod

    Murgilod

    Joined:
    Nov 12, 2013
    Posts:
    10,160
    This is probably the better way to do things, if not just because it's easier than dealing with the component stack directly.
     
    ryanawe likes this.
  7. MadeFromPolygons

    MadeFromPolygons

    Joined:
    Oct 5, 2013
    Posts:
    3,983
    Its perfectly fine, but if you have to put more than like 2-3 of the same thing, perhaps consider condensing them into a single component that has a public enum that allows you to switch what "type" it is.

    So if it was a damage status effect component, instead of having ParalyzeComponent, BleedingComponent, BurnedComponent etc

    You would have DamageStatusEffectComponent which would contain logic for all 3, with the public enum allowing you to switch which logic to actually use.

    Something like:

    Code (CSharp):
    1. public class DamageStatusEffect : Monobehaviour
    2. {
    3.     public enum StatusType
    4.    {
    5.       Paralyze,
    6.       Bleed,
    7.       Burned
    8.    }
    9.  
    10. // use this via inspector to switch the type
    11. public StatusType statusType;
    12.  
    13. public void Execute()
    14. {
    15.      if(statusType == StatusType.Paralyze)
    16.     {
    17.       // do something
    18.      }
    19.      else  if(statusType == StatusType.Bleed)
    20.     {
    21.       // do something else
    22.     }
    23.   else  if(statusType == StatusType.Burned)
    24.     {
    25.       // do something else
    26.     }
    27.  
    28. }
    29.  
    30. }
    Note that is just an example and is written freehand so likely to produce errors.
     
    ryanawe likes this.