Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

inspector var with Array()

Discussion in 'Scripting' started by Jean-Fabre, Sep 7, 2007.

  1. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    me again... but for a different topic:

    how can I create an array var so that it appears in the inspector as a popup with array content to choose from.


    typical example:
    a light has a type var that you can set to Point, directionnal or spot.

    I'd like to expose the same thing within my script.

    Thanks,

    Jean
     
  2. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    The light type is an enumeration. An enumeration is an integer that has names applied for each number. Enumerations make it easier to create readable code and efficiently switch between states in a script.

    Here is how to write an enumeration and a variable that uses it in javascript:

    Code (csharp):
    1.  
    2. var bored = false;
    3. var confused = true;
    4. var forumReadTime = 80.00;
    5.  
    6. enum EnumerationKnowledge {DoesntKnowWhatAnEnumIs = 0, IsLearningAboutEnums = 1, KnowsHowEnumsWork = 2,  InventorOfEnums = 3}
    7. var jeanfabresEnumKnowledge = EnumerationKnowledge.DoesntKnowWhatAnEnumIs;
    8.  
    9. function Start ()
    10. {
    11.    while(bored || confused)
    12.    {
    13.       yield ReadUnityForum();
    14.    }
    15. }
    16.  
    17. function ReadUnityForum ()
    18. {
    19.    if(jeanfabresEnumKnowledge == EnumerationKnowledge.IsLearningAboutEnums) jeanfabresEnumKnowledge = EnumerationKnowledge.KnowsHowEnumsWork;
    20.  
    21.    if(jeanfabresEnumKnowledge == EnumerationKnowledge.DoesntKnowWhatAnEnumIs) jeanfabresEnumKnowledge = EnumerationKnowledge.IsLearningAboutEnums;
    22.  
    23.    yield WaitForSeconds(forumReadTime);
    24. }
    25.  
    And yes that code should actually compile and work :)
     
  3. Jean-Fabre

    Jean-Fabre

    Joined:
    Sep 6, 2007
    Posts:
    429
    there you go!

    Strange tho when I search for enum in the api it doesn't return anything.

    Thanks,

    Jean
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    That's because it's a language feature, not something specific to Unity. If you like, you can leave out the number assignments, since they are implicit:

    Code (csharp):
    1. enum Button {Start, Stop, Forward, Backward}
    --Eric
     
  5. bronxbomber92

    bronxbomber92

    Joined:
    Nov 11, 2006
    Posts:
    888
    Enums don't have to be integers though (at least in C#).

    Anyone know if Unity's JS works the same as C# with enums?
    If so, here is some documentation. http://msdn2.microsoft.com/en-us/library/sbbt4032(VS.80).aspx