Search Unity

Dropdown in inspector?

Discussion in 'Scripting' started by asdfasdf234, Apr 29, 2017.

  1. asdfasdf234

    asdfasdf234

    Joined:
    Nov 27, 2015
    Posts:
    76
    Hi, so I just started writing a script and figured there was a lot of variables and would really like to use a dropdown menu so is there a way to make a dropdown menu in inspecor how?

    Code (CSharp):
    1.    
    2.     public bool isTrigByPlayerCOL;                  //menu 1 - item 1
    3.     public string playerTag = "Player";
    4.     [Space]
    5.     public bool isTrigByOBJ;                             //menu 1 - item 2
    6.     public GameObject trigByOBJ;
    7.     [Space]
    8.     public bool isTrigByTrig;                              //menu 1 - item 3
    9.     [Space]
    10.     [Header("What to do")]
    11.     [Space]
    12.     public bool enable_disable_GO = false;           //menu 2 - item 1
    13.     public GameObject GO_ToDisab;
    14.     public float disable_enable_after = 0f;
    15.     [Space]
    16.     public bool rotateOBJ = false;                           //menu 2 - item 2
    17.     public GameObject OBJ_toRotate;
    18.     public char rotationAxis;
    19.     public float rotateBy = 0;
    20. ......
    21.  
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Mehrdad995 and Alex-Koba_Ownused like this.
  3. ShawnFeatherly

    ShawnFeatherly

    Joined:
    Feb 22, 2013
    Posts:
    57
    ZapperCZ likes this.
  4. fire7side

    fire7side

    Joined:
    Oct 15, 2012
    Posts:
    1,819
    It's all right to start with a lot of public variables, but once you get them figured out, it's better to make them private so they aren't accidentally changed. Most of those bools, for instance, might be nice if you can see them for debugging, but once you get the program running well, you can make them private.

    There are reasons for making a list drop down, in which case you can use System.Serializable, or use an array, but just for your variables, I would just make a lot of them private, personally.
     
  5. RameyS

    RameyS

    Joined:
    Dec 19, 2018
    Posts:
    1
    I had the same problem and was spending lots of time on it, too. The solution is to make a serializable class inside your script's class and make an instance of that Serializable class. If you don't make an instance of it, the variables will not show up in the Inspector. I read some of the Unity documentation for this, but they say to use the PropertyDrawer thing, which is probably more powerful, but this is just a simpler, and possibly easier solution. With the instance, just get all your variables from that instance. Here's an example:

    Code (CSharp):
    1.  
    2. using System; //Needed for [Serializable]
    3. using UnityEngine;
    4.  
    5. public class ScriptClass : MonoBehaviour
    6. {
    7.     [Serializable]
    8.     public class VariableHolder
    9.     {
    10.         public bool var1;
    11.         public float var2 = 150f;
    12.         public float var3 = 25f;
    13.     }
    14.  
    15.     public VariableHolder instance = new VariableHolder(); //This is what makes the dropdown menu appear in the Inspector. It will have a dropdown arrow and the name of the instance.
    16. //Then, you would just reference the variables by saying float hi = instance.var2; for example
    17. }
    18.  
     
    d_gul, Foestar, procheckmate and 3 others like this.