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 /
This question was closed Mar 12, 2018 at 04:13 PM by Martinez_.
avatar image
0
Question by Martinez_ · Mar 08, 2018 at 01:12 PM · arraysrandomizationjagged

Selecting random array

I am trying to create a system where upon starting the game map will be selected and then generated from lets say map pool. All my maps are jaggedArrays. I tried to put jaggedArrays into ArrayList and generate from that but that did not work then I tried putting all my maps into 1 jaggedArray which look like this.

  maps[0] = new string[] { null, null, null, null, null, null, null, null, null, "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "eol"}
 
  maps[1] = new string[] { null, null, null, null, null, null, null, null, null, "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "W", "eol"}

"eol" - represents end of line after "eol" map information continues on another row.

(map is 24 by x and rows are limited by "eol" - x is undefined number)

Converting part of jaggedArray into separate array

    // vyberMapy transates to chooseMap
  
  public static void vyberMapy()
     {
         int number = Random.Range(1, 3);
 
         int i = 0;
         for (int j = 0; j < maps[number].Length; j++)
         {
             map1[i][j] = maps[number][j].ToString();
 
             if (maps[number][j].ToString() == "eol")
             {
                 i++;
             }
         }
     }

Conclusion I need to figure way to randomly select one of there maps, via randomly generated number atm. of range between 1-2 and using return function to return chosen map.

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

1 Reply

  • Sort: 
avatar image
1

Answer by Zodiarc · Mar 08, 2018 at 01:11 PM

I don't really understand what you're trying to achieve, but have you tried

 return map[Random.Range(0, map.Length)];

This will return a random "line" of your map array. Note that the max value is exclusive so you don't need to subtract 1 from it to prevent ArrayIndexOutOfRangeExceptions.

So let's say you have the following construct:

 ArrayList<string[]> maps = new ArrayList<string[]>();
 
 maps.Add(new string[]{1,2,3,4,5,6,7,8});
 maps.Add(new string[]{8,7,6,5,4,3,2,1});
 maps.Add(new string[]{5,6,7,8,1,2,3,4});
 

you could do the followoing:

 ArrayList<string[]>() generatedMap = new ArrayList<string[]>();
 
 for(int i = 0; i < <yourNumberOfLines>; i++) {
     generatedMap.Add(maps[Random.Range(0,maps.Count)]);
 }


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

Follow this Question

Answers Answers and Comments

74 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

Related Questions

How to make certain elements in an array rarer when using random selection? 4 Answers

Writing Jagged Arrays 1 Answer

When attempting to serialize and save a jagged array it gives me an error message 1 Answer

Changing Questions from static to variable driven.. 0 Answers

Syntax to initialize 2D (Jagged), built-in array at runtime 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