Search Unity

how do I use enum to make multiple choises in an ispector

Discussion in 'Scripting' started by stakensj, Nov 21, 2021.

  1. stakensj

    stakensj

    Joined:
    Oct 23, 2020
    Posts:
    35
    So, I am trying to make be able to choose what kind of transmission a car has using enum, but it doesn't work for me.
    Here is the script:
    Code (CSharp):
    1. // the choice of a transmission
    2.  
    3. public enum CarTransmisionType
    4.     {
    5.         AWD,
    6.         FWD,
    7.         RWD,
    8.     }
    9.  
    10. //the use of it
    11. private void HandleMotor()
    12.     {
    13.  
    14.        if (CarTransmisionType.AWD)
    15.        {
    16.             FLWC.motorTorque = VerticalInput * motorForce;
    17.             BLWC.motorTorque = VerticalInput * motorForce;
    18.             FRWC.motorTorque = VerticalInput * motorForce;
    19.             BRWC.motorTorque = VerticalInput * motorForce;
    20.        }
    21.  
    22.        if (CarTransmisionType.RWD)
    23.        {
    24.             BLWC.motorTorque = VerticalInput * motorForce;
    25.             BRWC.motorTorque = VerticalInput * motorForce;
    26.        }
    27.  
    28.  
    29.        if (CarTransmisionType.FWD)
    30.        {
    31.             FLWC.motorTorque = VerticalInput * motorForce;
    32.             FRWC.motorTorque = VerticalInput * motorForce;
    33.        }
    34.  
    35.         currentBreakForce = isBreaking ? breakForce : 0f;
    36.         ApplyBreaking();
    37.     }
    please help
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,491
    Saying "doesn't work" isn't a description of a problem but I can see your previous posts where you just state "doesn't work" too and was told the same (you didn't respond to the help provided either).

    Please spend more time describing your problems on what you expect to happen and what actually happens. Without that, devs have to somehow dry-run your code in their head without being able to use your project.

    Did you try to type "c# enum" or anything like that in Google. It gives you pages of help there with examples on how to use enums in C#.

    Nevertheless, you code in those "if" statements make no sense. You actually need to store an enum state in a field then check if the field is set to one of those values. This is no different than an "int" or "float" except an enum allows you to specify the exact contents allowed.

    Code (CSharp):
    1. CarTransmisionType myTransmissionType = CarTransmisionType.AWD);
    2.  
    3. if (myTransmissionType == CarTransmisionType.RWD)
    4. {
    5.     ....
    6. }
     
    Bunny83 likes this.
  3. stakensj

    stakensj

    Joined:
    Oct 23, 2020
    Posts:
    35
    Thank You, I am sorry for describing my problems like that and from now on will try to put in more information. I tried searching in google but couldn't find any solution to my problem Thank you for explaining it for me
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,736
    Keep in mind that enums are extremely fragile in Unity3D if you intend them to be serialized:

    https://forum.unity.com/threads/bes...if-not-do-something-else.972093/#post-6323361

    https://forum.unity.com/threads/unity-card-game-structure.1006826/#post-6529526

    It is much better to use ScriptableObjects for many enumerative uses. You can even define additional associated data with each one of them, and drag them into other parts of your game (scenes, prefabs, other ScriptableObjects) however you like. References remain rock solid even if you rename them, reorder them, reorganize them, etc. They are always connected via the meta file GUID.
     
    Bunny83 likes this.
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Defining an enum is the same as defining a class or other type.
    You need to then create a variable of your enum type in a script to use it:
    Code (CSharp):
    1. public enum MyEnum
    2. {
    3.   Option1,
    4.   Option2,
    5.   Option3
    6. }
    Code (CSharp):
    1. public class MyScript : MonoBehaviour
    2. {
    3.   public MyEnum myEnum;
    4. }
     
    MelvMay and Bunny83 like this.