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 Jacob_Anthony · May 01, 2017 at 02:25 PM · c#arraylist

Logic problem with lists C#

i wanted to make gameobject list or array with all possible combination of enemies for example i have 3 enemies a, b and c and list should look like this: a, b, c, a+b, a+c, c+b, a+b+c, I tried to make this by list of custom class or multidimensional array but it doesn't work (I don't know how many enemies are there)

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

2 Replies

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

Answer by dpoly · May 02, 2017 at 02:10 AM

What you are describing is a 'combination'

"Alice, Bob and Charlie is the same as Charlie, Bob and Alice. Permutations are for lists (order matters) and combinations are for groups (order doesn't matter). A joke: A "combination lock" should really be called a "permutation lock". The order you put the numbers in matters."

There are specific algorithms to create a list that is a combination of a set of elements (web search it). You can't do it with a simple loop; essentially you either need to loop over an array of indexes into a list or a recursive function. Challenging stuff, if you're not an expert.

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
2

Answer by UnityCoach · May 01, 2017 at 08:59 PM

List of Lists would work, and multi-dimension arrays would work too.

 GameObject [][] referencesArray;
 referencesArray[0][0] = obj;
 for (int i = 0 ; i < referencesArray.Length ; i++)
     for (int g = 0 ; g < referencesArray[i].Length ; g++)
         Debug.Log (g.name);

 List<List<GameObject>> referencesList = new List<List<GameObject>>();
 referencesList.Add (new List<GameObject>());
 referencesList[0].Add (obj);
 foreach (List l in referencesList)
     foreach (GameObject g in l)
         Debug.Log (g.name);

When you say it doesn't work, do you mean you can't assign them in the editor?

Comment
Add comment · Show 4 · 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 Jacob_Anthony · May 02, 2017 at 07:15 PM 0
Share

i mean that multi-dimensional array can't have 1 variable in first line and 3 in second(am i right?) and the only solutions i figured out with list of lists is to put them in foreach loop like this

 GameObject[] enemylist;
 List<List<GameObject>> listoflists;
 List<GameObject> oneoflists;
 public void $$anonymous$$akeListOfLists()
     {
         foreach (GameObject enemy in enemylist)
         {
                         oneoflists.Clear();
             oneoflists.Add(enemy);
             listoflists.Add(oneoflists);
         }
         if (enemylist.Length > 1) {
             foreach (GameObject enemy1 in enemylist)
             {
                 foreach (GameObject enemy2 in enemylist)
                 {
                     if (enemy1 != enemy2)
                     {
                         oneoflists.Clear();
                         oneoflists.Add(enemy1);
                         oneoflists.Add(enemy2);
                         listoflists.Add(oneoflists);
                     }

} } } } }

but it don't make sense because if there gonna be more enemies i'll must add and add loops

avatar image dpoly Jacob_Anthony · May 03, 2017 at 12:18 AM 0
Share

You are correct, that is exactly the problem. And the solution is as per my answer. You can write an iterative solution using an array of loop indexes or a recursive solution using variable depth, but you cannot do it as above. Do you understand this, or is it just too hard?

avatar image Jacob_Anthony dpoly · May 03, 2017 at 08:26 AM 0
Share

i think i don't but i'll try to make research thx :P

Show more comments

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

9 People are following this question.

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

Related Questions

How to get all children of a Gameobject with a certain component 2 Answers

Array with pushing values? 0 Answers

Array (List) with multiple variable types? 2 Answers

Assign role randomly from array for my online game 2 Answers

How to declare a list of arrays? 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