Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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
1
Question by SeaLeviathan · Jul 24, 2017 at 02:33 AM · uigameobjectarraydropdown

How to make dynamic dropdown to choose items in an Array?

Hi, I'm wondering how exactly I could go about making a dynamic dropdown menu for a really big gameobject array I have.

I made 107 gameobjects for a building game prototype I recently started. I loaded them all into a GameObject array called ghosts, and i can cycle through them by changing the value of the array, like ghosts[0] to ghosts[1] or ghosts[2] etc. I am wondering if there is a way to put these objects into a ui dropdown list, and most importantly if i can set each dropdown option to change my array value, so if I click the third down option in the list it can change ghosts[0] to ghosts[2].

I'm open to whatever suggestions anyone comes up with, I just don't know exactly how to 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
5
Best Answer

Answer by Kishotta · Jul 24, 2017 at 05:10 AM

You can use something like this to programatically populate a Dropdown:

 void PopulateDropdown (Dropdown dropdown, GameObject[] optionsArray) {
     List<string> options = new List<string> ();
     foreach (var option in optionsArray) {
         options.Add(option.name); // Or whatever you want for a label
     }
     dropdown.ClearOptions ();
     dropdown.AddOptions(options);
 }


usage:

 Dropdown myDropdown;
 GameObject[] dropdownObjects = GameObject.FindGameObjectsWithTag ("PowerUp"); // For example
 PopulateDropdown (myDropdown, dropdownObjects);

Note: This can be pretty slow if you have a ton of items. Because Dropdown.AddOptions() doesn't get rid of any existing options, you may be able to do this one item at a time in a coroutine as opposed to doing it all at once.

Comment
Add comment · Show 7 · 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 SeaLeviathan · Jul 24, 2017 at 05:41 AM 0
Share

I will try this out in the morning, thank you! On my own I was trying to convert my object array to a list with each array objects name, but it doesn't seem to be going anywhere. Thanks much for the suggestion!

avatar image SeaLeviathan · Jul 24, 2017 at 01:49 PM 0
Share

Ok I tried this out and it did the first part exactly as I wanted it to. I think I know how to make it so that each selection sets my array[var] to that specific object. If you want to try and help me with that you can, but until then I am accepting this as the answer!

avatar image SeaLeviathan · Jul 25, 2017 at 04:50 AM 0
Share

@$$anonymous$$ishotta if you get this, I am wondering why the dropdown.AddOptions and ClearOptions are returning a "Could not find reference" error, even though after unpausing the game after this error it works perfectly fine?

the code by itself works fine, but I went into my other project and it started throwing this weird error every time I want to use the PopulateDropdown function, I just have no idea why.

 void PopulateDropdown(Dropdown dropdown, GameObject[] optionsArray)
     {
         List<string> options = new List<string>();
         foreach (var option in optionsArray)
         {
             options.Add(option.name); // Or whatever you want for a label
         }
         
         dropdown.ClearOptions();
         dropdown.AddOptions(options);
     }
     private void Awake()
     {
         Dropdown myDropdown = GetComponent<Dropdown>();
         GameObject[] dropdownObjects = Resources.LoadAll<GameObject>("Prefab/Ghost/Ghost " + path) as GameObject[]; // For example
 
         PopulateDropdown(myDropdown, dropdownObjects);
     }

this is where i call it, but it throws the stupid reference error, even though not only does it work, but it has no reason to not find the Dropdown in reference.

avatar image Kishotta SeaLeviathan · Jul 25, 2017 at 05:39 AM 0
Share

Odd that it would be erroring out but also working....

Can you post the complete error you get from the console?

avatar image SeaLeviathan Kishotta · Jul 25, 2017 at 03:59 PM 0
Share

NullReferenceException: Object reference not set to an instance of an object $$anonymous$$enu.PopulateDropdown (UnityEngine.UI.Dropdown dropdown, UnityEngine.GameObject[] optionsArray) (at Assets/Script/$$anonymous$$enu.cs:23) $$anonymous$$enu.Awake () (at Assets/Script/$$anonymous$$enu.cs:31)

the line 23 would be dropdown.ClearOptions();, but if i get rid of that it moves to .AddOptions so i figure for some reason it cant find the dropdown? but that makes no sense because it finds the dropdown, and populates the list. line 23 is just calling the function.

Show more comments
avatar image
0

Answer by codemaker2015 · Sep 22, 2021 at 01:03 PM

You can use AddOptions() method to add dropdown options dynamically. There we have specify a list of string that needs to display on the dropdown.

 [SerializeField]
 Dropdown dropdown; 
 
 public void Start() {
     List<string> options = new List<string> () { "Option1", "Option2", "Option3" };
     AddDropdownOptions(dropdown, options);
 }
 
 void AddDropdownOptions(Dropdown dropdown, List<string> options) {
         dropdown.ClearOptions ();
         dropdown.AddOptions(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

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.

Follow this Question

Answers Answers and Comments

136 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

Related Questions

dropdown uses a generic list and not a built-in list -- why? 0 Answers

instantiate gameobjects inside a canvas 0 Answers

Gathering GameObjects and then creating a button for each GameObject?? 2 Answers

New Unity Dropdown Menu to Change Game Resolution 1 Answer

Use an objects (from array) position to focus a camera on 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges