Create group people, Select one or more at random.
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.
Bob, 25, 150; Mike, 33, 175; Jenny, 18, 110; Susan, 20, 115;
I simply don't know how to create the class or list (etc.) that includes these items and then select a person and display their proper age and weight.
I've been searching high and low and so far, I've tried everything I know and can't quite get any of it to work.
I've been reading everything I can on custom class creation and access but I think there is something basic I'm missing. I'll keep at it until then. I would still like to hear an answer on the above question if anyone gets a chance.
Thanks.
First in my opinion if you cant understand this, you dont need it for now. Only if you just want learn more about coding, but if you just make game i think this is not so important thing. Do you check official unity tutorial on this? Im not pro at this but try explain. This script dont need to be attached to GameObject
using UnityEngine;
using System.Collections;
public class human : $$anonymous$$onoBehaviour{
public string name;
public int age;
public void scream(){
print (name + " scream his age " + age);
}
//Constructors
public human (){
name = "default";
age = 5;
}
public human (int _age){
name = "default";
age = _age;
}
public human (int _age, string _name){
age = _age;
name = _name;
}
}
add this code to your player Start function. Start the game and check console log. Try to change and practice something.
human person1 = new human ();
human person2 = new human (2);
human person3 = new human (2, "idiot");
person1.scream ();
person2.scream ();
person3.scream ();
And for list use "using System.collection.generic" then
public list<human> peoples = new list<human>();
peoples[Random.Range(0, peoples.Count - 1)].scream();
i think this should be work
Is there a way to add a bunch of random members to this class...if so, I don't know the syntax.
$$anonymous$$aybe something like
// Constructor public human (string _name) { age = 10; name = _name; }
// Then, in start, I want to make 20 of these, this doesn't work but I'm trying to figure out some way to do this.
for (human i = 0; i < 20; i++)
{
human person[i] = new human($$anonymous$$r+i);
}
Your answer is fabulous and it helped me greatly to understand more about class and list. I really appreciate that you took the time to help me. I'm going to take some time and play around with what you gave me so I can understand it more.
I've never added a "drop down" menu so I'm a little fuzzy on that but I'll try to figure out how to do that. Thanks again.
Answer by TBruce · Nov 15, 2016 at 02:51 AM
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[i].name, people[i].age, people[i].weight)));
}
text.text = dropDown.options[0].text;
dropDown.value = 0;
}
}
void AddPerson(string _name, int _age, float _weight)
{
Person person = new Person(_name, _age, _weight);
people.Add(person);
}
}
I've never added a drop down menu but I will give this a try. Thank you for taking the time to help me.