Hello, i’m from Poland, sorry for my bad English
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);
}
}
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.
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
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
}
}
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.
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.
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?