dropdown with value

Hello, i’m from Poland, sorry for my bad English :slight_smile:
I watch this tutorial -

and i have script from it ^^ How can I change this to display several values instead of names?
I’m using unity from 2 days and i’m beginner in scripting.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DropDownExample : MonoBehaviour
{

    List<string> names = new List<string>() { "Nothing", "Barney", "Wilma", "Betty" };

    public Dropdown dropdown;
    public Text selectedName;
    public void Dropdown_IndexChanged(int index)
    {
        selectedName.text = names[index];
        if(index==0)
        {
            selectedName.color = Color.red;
        }
        else
        {
            selectedName.color = Color.white;
        }
    }

    private void Start()
    {
        PopulateList();
    }

    void PopulateList()
    {
        dropdown.AddOptions(names);
    }



}

Hi you need to replace

List<string> names = new List<string>() { "Nothing", "Barney", "Wilma", "Betty" };

with

List<int> numbers = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

or

var numberList = Enumerable.Range(1, 10).ToList();

That’s not what I meant. This will be explained in the image below. {translator}

I think slider will suit your task better

check out this link https://docs.unity3d.com/ScriptReference/UI.Slider.html

You can have 3 sliders HP, AP, PA (or any number your want), If you change the value in one slider you can program the other sliders to change.

Could you give me some sample script? I’m using UNITY only 2 days, and I do not know how to use it :frowning:
I used to use AutoIT but it’s not the same thing.

Now I see that slider is not a good solution. Because I want to have xx items in drop down list. And each of these items will have a value of HP / PA / AP.

You’re going to have to do a check for when a value is selected in the drop down, you check what value is selected and you have your hp/ap/pa values tied to it. Either an if check or a switch or something so when something is selected you get those values.

Could you give an example script how it looks? As I said I am a beginner at UNITY.

Dropdown AddOptions method only takes in strings so you’ll need to convert whatever you want to display there to a string. Luckily integers are fairly easy to convert to string and back so you can just use ToString() method.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class DropDownExample : MonoBehaviour
{
    List<int> numbers = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

    public Dropdown dropdown;
    public Text selectedName;
    public void Dropdown_IndexChanged(int index)
    {
        selectedName.text = numbers[dropdown.value].ToString();
        if (dropdown.value == 0)
        {
            selectedName.color = Color.red;
        }
        else
        {
            selectedName.color = Color.white;
        }
    }

    private void Start()
    {
        PopulateList();
    }

    void PopulateList()
    {
        dropdown.ClearOptions(); //Clear pre-added options from the list, remove this line if you want to keep em.
        List<string> newOptions = new List<string>();
        for (int i = 0; i < numbers.Count; i++)
        {
            newOptions.Add(numbers[i].ToString());
        }
        dropdown.AddOptions(newOptions);
    }
}

For some reason the Dropdown_IndexChanged always returned 0 for me so just used the actual value from the dropdown instead

1 Like

I do not mean to display numbers in the menu. The menu will be:

  1. Sun Amulet, and he has assigned different values AP = 10, PA = 20, which I want to display under the drop down list.

Oh my bad, listing objects is even easier as long as they have names. You can create serializable Class that contains the data you need and fetch and display it’s details when index changes.

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

[System.Serializable]
public class Item
{
    public string Name = "";
    public int HP = 0;
    public int AP = 0;
    public int PA = 0;
}

public class DropDownExample : MonoBehaviour
{
    //You can polate the list from inspector
    public List<Item> Items = new List<Item>() {
        new Item() { Name = "First Item", AP = -50, PA = 25, HP = 10 },
        new Item() { Name = "Second Item", AP = 30, PA = 100, HP = -25 },
        new Item() { Name = "Sun Amulet", AP = 10, PA = 20, HP = 0 }
    };

    public Dropdown dropdown;
    public Text selectedName;
    public void Dropdown_IndexChanged(int index)
    {
        Item selected = Items[dropdown.value];
        selectedName.text = selected.Name
            + "\n HP = " + selected.HP.ToString()
            + "\n AP =" + selected.AP.ToString()
            + " \n PA =" + selected.PA.ToString(); 
    }

    private void Start()
    {
        PopulateList();
    }

    void PopulateList()
    {
        dropdown.ClearOptions();
        List<string> newOptions = new List<string>();
        for (int i = 0; i < Items.Count; i++)
        {
            newOptions.Add(Items[i].Name);
        }
        dropdown.AddOptions(newOptions);
        Dropdown_IndexChanged(0); //select first
    }
}
2 Likes

I have a problem with this script, namely, it displays “First Item HP = 10”, I choose second item or sun amulet, and still see “First Item HP = 10”.

Did you tie the method to the on value changed call in the dropdown object in the inspector?

2 Likes

When it comes to adding DropDown objects and Text to the script.
I added.

Not what I asked. I asked if you tied the method into the on value changed part of the dropdown object in your scene. You need to select your dropdown, then Dropdown_IndexChanged needs to be in the on value changed part.

I’m not sure the purpose of the index parameter though, so you probably don’t need that.

If you are new to Unity and this doesn’t make sense to you, I would suggest starting with some tutorials as this is part of the basics of Unity and the tutorials will really help you out.

1 Like

Sorry I’m stupid and I do not understand it! But I want to learn it, I added everything but it does not work. Look at the screenshot:

This has nothing to do with your intellect level. It has to do with taking the time to follow tutorials and learn the Unity system.

As far as why it doesn’t work, you still most likely do not have the method set up in the on value changed event.

Select your drop down and you’ll see the image I posted in the inspector. Note the “On Value Changed” section. You need to attach the script there and select the method. This is why I mention doing tutorials as this is part of the most basic part of Unity. You’ll come across doing this sort of thing for buttons, scrolls rect, and several other things (usually with UI stuff, but other things are possible)

It’s great to have a willingness to learn! Just remember to take the time to do so. If you’re new, practice. Unity has a ton of great resources out there.

3131205--237200--dropDown.PNG

1 Like

Haha bro! I’m very stupid! Only now did I understand that I should add the script not only to the empty object but also to the dropdownlist. At first I did not understand this, probably because my English is at zero level and I’m forced to use a translator. You wrote me to watch guides, do you have any worth recommending?

And, I forgot to add I LOVE YOU! Thank you for having so much patience for me: D

/edit
And you know maybe some interesting tutorial about Android 2d application in the unity?
/edit2
Can I add a picture in this script? To display a text and a photo for a given selection in a dropdown list?