Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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

avatar image Topthink callme · Nov 15, 2016 at 06:14 PM 0
Share

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);
     }
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.

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

Several objects, same script, different values with no duplications 1 Answer

Get a selected item from a GameObject list? 0 Answers

Adding classes to objects 0 Answers

C# Find specific object by getting one of its variables 0 Answers


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