I’m trying to create a group of items with certain attributes and then select one item and show their attributes. The “group” can be a list, array, class, anything (whatever is the absolute easiest to work with so long as I can add and remove items at will).
For the sake of argument, say I have four people along with their age and weight.
This will work for you (I added a UI.Dropdown list)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
[System.Serializable]
public class Person
{
public int id;
public string name;
public int age;
public float weight;
public Person(string _name, int _age, float _weight)
{
name = _name;
age = _age;
weight = _weight;
}
}
public class PersonIndexer : MonoBehaviour
{
public List<Person> people = new List<Person>();
public Dropdown dropDown;
private Text text;
void Start()
{
AddPerson("Bob", 25, 150);
AddPerson("Mike", 33, 175);
AddPerson("Jenny", 18, 110);
AddPerson("Susan", 20, 115);
if (dropDown != null)
{
text = dropDown.gameObject.GetComponentsInChildren<Text>()[0];
dropDown.options.Clear();
for (int i = 0; i < people.Count; i++)
{
dropDown.options.Add(new Dropdown.OptionData(string.Format("Name: {0}, Age: {1} Weight: {2}", people<em>.name, people_.age, people*.weight)));*_</em>