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 /
avatar image
0
Question by perdamcivitatem · May 02, 2018 at 05:44 PM · listlistsvalues

how can I get values stored in a list?

How could I get the random values stored in the list, to then calculate what the average is?

     gamingTypes.Add (new Gaming ("Board Games", Random.Range(-5, 5)));
     gamingTypes.Add (new Gaming ("Computer Games", Random.Range(-5, 5)));
     gamingTypes.Add (new Gaming ("Phone Games", Random.Range(-5, 5)));
     gamingTypes.Add (new Gaming ("Card Games", Random.Range(-5, 5)));
Comment
Add comment · Show 2
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 Kciwsolb · May 02, 2018 at 06:21 PM 1
Share

The $$anonymous$$icrosoft docs are probably the best place to check for basic C# questions:

https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1?view=netframework-4.7.1

Check the public methods available and note that you can just access by index with square brackets (like an array).

If you are asking how to iterate through a List, that is a basic program$$anonymous$$g question and I highly suggest you look up some tutorials on basic C# program$$anonymous$$g. The gist is to use a for loop and use your iterator to access each index in the List. Then use each one in what ever math you want.

Like add each one to your sum variable, and then at the end of the loop divide by how many values are in your List.

avatar image perdamcivitatem Kciwsolb · May 03, 2018 at 02:13 AM 0
Share

Thanks for the advice and link!

What messed it up for me was having the string and int values in each index. Also, hadnt used lists before, and the unity videos + other questions werent helping.

Gonna check the link, thanks a lot!

3 Replies

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

Answer by Koyemsi · May 02, 2018 at 06:17 PM

Assuming that your Gaming class looks like this :

 public class Gaming
 {
     public string str;
     public int val;
     
     public Gaming(string newStr, int newVal)
     {
         str = newStr;
         val = newVal;
     }
 }

Here is how you can calculate average :

 public class AverageFromList : MonoBehaviour
 {
     List<Gaming> gamingTypes = new List<Gaming> ();
     int total = 0;
     void Start ()
     {
         gamingTypes.Add (new Gaming ("Board Games", Random.Range (-5, 5)));
         gamingTypes.Add (new Gaming ("Computer Games", Random.Range (-5, 5)));
         gamingTypes.Add (new Gaming ("Phone Games", Random.Range (-5, 5)));
         gamingTypes.Add (new Gaming ("Card Games", Random.Range (-5, 5)));
         
         foreach (Gaming g in gamingTypes)
         {
             print (g.str + " : " + g.val);
             total += g.val;
         }
         print ("Total : " + total);
         float average = (float)total / gamingTypes.Count;
         print ("Average : " + average);
     }
 }

Comment
Add comment · Show 9 · 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 Bunny83 · May 02, 2018 at 06:43 PM 1
Share

$$anonymous$$eep in $$anonymous$$d that since your "total" is an integer you get a "floored" average. So the values

 2, 1, 3, -5

Would add up to "1". If you divide 1 by 4 you get 0 ins$$anonymous$$d of 0.25 which is the actual average. That's because you do an integer division since both: "total" and "ga$$anonymous$$gTypes.Count" are integer values so the result is an integer as well. To get a floating point result at lease one of the operands need to be a float. One way would be to define total as float. Another would be

 float average = (float)total / ga$$anonymous$$gTypes.Count;
avatar image perdamcivitatem Bunny83 · May 02, 2018 at 07:08 PM 0
Share

Would using $$anonymous$$athf.RoundToInt work? I need an int, because the list of the activities in set in int, and I want the average to show a real-ish stat in ga$$anonymous$$g. Although I thought of just having the kinds of games on their own, ins$$anonymous$$d of them being inside the ga$$anonymous$$g list. What are your thoughts on this?

On a side note: I've assigned how much a person would like to do x activity. Using buttons you can decide what to ask the npc what it wants to do. How would you make it so each button would check the value the npc has for that activity?

avatar image Koyemsi perdamcivitatem · May 02, 2018 at 11:11 PM 0
Share

RoundToInt does work :

  float average = (float)total / ga$$anonymous$$gTypes.Count;
  print ("Average : " + average);
     
  float averageInt = $$anonymous$$athf.RoundToInt (average);
  print ("Average integer : " + averageInt);

I probably would keep the ga$$anonymous$$g kinds in the list if possible, because it keeps them "linked" with their statistics. And please excuse me, but I didn't understand the other question very well.

Show more comments
avatar image Koyemsi Bunny83 · May 02, 2018 at 11:01 PM 0
Share

You're absolutely right @Bunny83, thanks. I've updated my code.

avatar image perdamcivitatem · May 02, 2018 at 06:51 PM 0
Share

Thanks! Didn't know I could use g.str/val to get the info I wanted.

Doesn't have to do with this, but I've assigned how much a person would like to do x activity. Using buttons you can decide what to ask the npc what it wants to do. How would you make it so each button would check the value the npc has for that activity? I think this has to do with being able to make a reference to other scripts, gonna be that rn, but I' appreciate your input!

Thanks again, and I hope you're having a good day/night!

avatar image Koyemsi perdamcivitatem · May 02, 2018 at 11:20 PM 0
Share

Didn't know I could use g.str/val to get the info I wanted.

Be careful, str and val are not methods. These are just the names I gave to my variables in the Ga$$anonymous$$g class, but I could have named them myString and myValue, or gameType and gameQuantity. When you feed your List, it's like assigning i.e. "Board Games" to the str variable, and a random int to the val variable.

Glad if I could help, have a nice day/night too.

avatar image perdamcivitatem Koyemsi · May 03, 2018 at 02:04 AM 0
Share

I know, i know. I meant that I forgot about accessing the info using the "g.". Thanks for explaining tho.

$$anonymous$$y question is: I have a list of activities like this:

     activity.Add (new Activities ("Fishing", Random.Range(-5, 5)));
     activity.Add (new Activities ("Running", Random.Range(-5, 5)));
     activity.Add (new Activities ("Hunting", Random.Range(-5, 5)));

I got buttons on the screen, with the names of the activities, and depending on the one you press, it would check the list, and get the value. How can I do this?

Sorry if this is really basic, been a while since the last time i programmed. Thanks a lot

avatar image
0

Answer by Koyemsi · May 03, 2018 at 11:59 AM

OK, I have the solution, here we go. First, the script : using System.Collections; using System.Collections.Generic; using UnityEngine;

 using UnityEngine.UI;
 
 
 public class Activities {
     public string activityName;
     public int activityValue;
 
     public Activities (string aString, int anInt) {
         activityName = aString;
         activityValue = anInt;
     }
 }
 
 
 public class ActivityManager : MonoBehaviour {
     List <Activities> activity = new List<Activities> ();
 
     void Start () {        
         activity.Add (new Activities ("Fishing", Random.Range (-5, 5)));
         activity.Add (new Activities ("Running", Random.Range (-5, 5)));
         activity.Add (new Activities ("Hunting", Random.Range (-5, 5)));
 
         foreach (Activities act in activity) {
             print (act.activityName + " : " + act.activityValue);
         }
     }
 
 
     public void ReadInList (Button aButton) {
         string stringToTest = aButton.GetComponentInChildren<Text> ().text;
         print ("Pressed " + stringToTest);
 
         var matchingEntry = activity.Find (Activities => Activities.activityName == stringToTest);
 
         if (matchingEntry == null) {
             print ("No such activity in List.");
         } else {
             print ("Found " + stringToTest + " with value : " + matchingEntry.activityValue);
         }
     }
 }

The script is attached to an empty GameObject that I called @ScriptHolder. Your buttons' OnClick have to call the ReadInList() method with an argument refering to themself. The script reads what's written in the button's Text (be sure to label them exactly the same as the activities stored in your List). And the magic happens ;)

alt text


screen-capture-1.jpg (21.3 kB)
Comment
Add comment · 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
0

Answer by widsmob1 · May 12, 2018 at 05:11 AM

count = 0; foreach (Gaming g in gamingTypes) { total += g.val; count++; } print ("Total : " + total); float average = (float)total / count print ("Average : " + average); }

Comment
Add comment · 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

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

87 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

How to make a simple gui list 0 Answers

How to add an item to a list with a class/uJS struct? 1 Answer

How to stop instantiating a certain object / objects from a list . 3 Answers

type or namespace name `List`1' could not be found 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