• 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 /
  • Help Room /
avatar image
0
Question by Topthink · Nov 14, 2016 at 07:35 AM · objectlistclassinstanceitem

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.

Comment
Add comment · Show 5
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 Topthink · Nov 15, 2016 at 02:09 AM 0
Share

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.

avatar image callme Topthink · Nov 15, 2016 at 02:30 AM 0
Share

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

Show more comments
avatar image Topthink · Nov 15, 2016 at 05:25 PM 0
Share

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.

avatar image Topthink Topthink · Nov 15, 2016 at 05:26 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
     }
 }
Comment
Add comment · Show 1 · 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 Topthink · Nov 15, 2016 at 05:33 PM 0
Share

I've never added a drop down menu but I will give this a try. Thank you for taking the time to help me.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

List of Class With GameObject 0 Answers

Can we anyhow store a prefab in a list when we collide into the instance of that prefab casted as gameObject? 1 Answer

How to determine class of object at mouse position 1 Answer

List check argument of item 3 Answers

List only fills after playmode 1 Answer


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