• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by richwarp · Jan 13 at 11:39 PM · scripting problem

How to pass a dropdown UI value to an enum in another script?

I am trying to pass a value from a UI dropdown object (using OnValueChanged) into another script, so that the value selected controls what enum value is selected in the inspector for that other script. For example, I would like my UI selection to select "Item 2" of the enum in the target script (the code for which is below):

 using UnityEngine;
 public class UpgradeModule : MonoBehaviour
 {
     public MyEnum myDropDown = new MyEnum();
 }
 
 public enum MyEnum
 {
     Item1,
     Item2,
     Item3
 };


How do I do this?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DanielGL · Jan 14 at 03:25 AM

You can convert the dropDown value to an enum, it's be something like this

 using UnityEngine;
 using UnityEngine.UI;
 
 public class GetDropDownValue : MonoBehaviour
 {
     public Dropdown dropdown;
 
     public enum DropDownSelection { A, B, C }
     public DropDownSelection selection;
 
     private void Awake()
     {
         dropdown.onValueChanged.AddListener(delegate { DropdownValueChanged(dropdown); });//Add listener to Event
     }
 
     void DropdownValueChanged(Dropdown change)
     {
         selection = (DropDownSelection)change.value; //Convert dropwdown value to enum
     }
 }

Edit: For populate a dropdown with an enum, you will need to use the "System" namespace to get acess to the "Enum" type, see:

 using System;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class DropDownPopulation : MonoBehaviour
 {
     public Dropdown dropdown;
 
     [System.Serializable]
     public enum FormatPresetType
     {
         RGB,
         CMYK
     }
     FormatPresetType formatPreset;
 
     private void Awake()
     {
         PopulateDropDownWithEnum(dropdown, formatPreset);
     }
 
     public static void PopulateDropDownWithEnum(Dropdown dropdown, Enum targetEnum)//You can populate any dropdown with any enum with this method
     {
         Type enumType = targetEnum.GetType();//Type of enum(FormatPresetType in my example)
         List<Dropdown.OptionData> newOptions = new List<Dropdown.OptionData>();
 
         for(int i = 0; i < Enum.GetNames(enumType).Length; i++)//Populate new Options
         {
              newOptions.Add(new Dropdown.OptionData(Enum.GetName(enumType, i)));
         }
 
         dropdown.ClearOptions();//Clear old options
         dropdown.AddOptions(newOptions);//Add new options
     }
 }

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by richwarp · Jan 14 at 10:36 AM

Thank you! This seems to work if I want to create an enum list within this script, but in my other script I already have an enum list, and I want the dropdown on the UI object to be populated with these values, so that when I make a change in the UI, it selects from that list in the script. Maybe I'm thinking about it wrong, but here's an example of what's actually in the script:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [AddComponentMenu("Color/ColorListener")]
 [RequireComponent(typeof(AudioListener))]
 [ExecuteInEditMode]
 public class ColorListener : MonoBehaviour
 {
     [System.Serializable]
     public enum FormatPresetType
     {
         RGB,
         CMYK
     }
 
     [System.Serializable]
     public enum PatternPresetType
     {
         Black,
         White,
         Red,
         Orange,
         Yellow,
         Blue,
         Custom
     }
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DanielGL · Jan 14 at 10:06 PM 0
Share

hmm, I guess now I understand, I have edited my answer, now with how to pass the enum values for the dropdown object, see if it works for you now... any questions, just ask ;)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

205 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

When I press "Edit Script" a popup says "Setup Error" and won't let my edit my script.,How to fix Unity Script Editor when it says "Setup Error." 1 Answer

How do you keep rotation constant no matter where you look? 1 Answer

How to stop certain gameobjects from interacting with triggers 1 Answer

How can i rotate the camera with the mouse but only when drag ? 0 Answers

When switching scenes some of my scripts stop working 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges