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

Resolved Is it possible to assign all enum types into one gameobject?

Discussion in 'Scripting' started by frankiwinnie, Dec 12, 2022.

  1. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Hello everyone I have class with an enum like:

    Code (CSharp):
    1. public class Card :  MonoBehaviour
    2. {
    3. public enum Color {Blue, Orange, Red, Grey}
    4. public Color color;
    5. }
    So what I want to do is to assign all four colors into one gameobject whereas the other ones shall have only 1 type assigned.

    Is it possible and how can I do that?

    Thank you very much.
     
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    You could just use a List to keep track of which Colors it has.

    Alternatively, since enums are just numbers, you could just assign it a bitmask.
     
  3. spiney199

    spiney199

    Joined:
    Feb 11, 2021
    Posts:
    5,842
    I don't remember off the top of my head if Unity supports flags enums in the inspector, but you could use a flag enum:

    Code (CSharp):
    1. [System.Flags]
    2. public enum Colour
    3. {
    4.     Blue = 1,
    5.     Orange = 2,
    6.     Red = 4,
    7.     Grey = 8
    8. }
    You will need to orient your code with this in mind, however. Refer to C#'s docs about this.
     
  4. frankiwinnie

    frankiwinnie

    Joined:
    Dec 1, 2020
    Posts:
    26
    Thanks for replies