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 ComicGirl · May 29, 2018 at 09:05 PM · randomarrayslistsrandom.rangeshuffle

List or Arrays, and how to randomize them.

I'm currently in a pickle, and I think writing it out and getting some advice is what I need. I have a pretty specific question it seems, and I only am more discouraged when trying to find answers, so here it goes-

The goal of the script that I want to create is a list or array of gameobjects ( or strings) that I can then use to randomly decide which order they will activate and when. For example, the player has 3 tasks they need to complete, task 1, task 2, task 3. I want to randomly pick which order the tasks come in. This way the player is not doing the taks in the same order.

I haven't worked with arrays or lists to know if I am able to do this. I was hoping for some general guidance on how to structure this. I don't have any code specific to this, but I would appreciate any guidance.

My thoughts were that I would be able to store the tasks as strings or gameobjects and have the script select them and store the order in variables, then when I need to reshuffle the tasks, overwrite those variables. However, I'm not sure how to put that in practice. Thanks!

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

· Add your reply
  • Sort: 
avatar image
0

Answer by tormentoarmagedoom · May 30, 2018 at 09:21 AM

Good day.

You want to pick random elements from an array, correct?

Then you need to do 2 things, one, learn to pick a random elemnt, and learn how to check if that element was already picked.

you have this array of 5 Gameobjects:

 GameObject[] MyArray = new GameObject[5]

If i want the 1st element of the array (index = 0)i do dins

 MyArray[0]

The 3rd element;

 Myarray[2]

So, if i want a random element, i need to do this:

 int RandomNumber = Random.Range(0,MyArray.Lenght)
 MyArray[RandomNumber]

So you will get a random element from ( 0 ) to (lenght of the array).

This is the 1st part.

As Random.Range doesn't know what elements are already selected, you need to check if the Random.Range is selecting a neew element.

So you should create a bool array, to know if a element has been selected.

 bool[] ConfirmationArray = new bool [MyArray.Lenght];

Every time, and element is selected, i will change to true the bool variable in the confirmation array.

 int RandomNumber = Random.Range(0,MyArray.Lenght)
 while (ConfirmationArray[RandomNumber] == true)
 { 
 RandomNumber = Random.Range(0,MyArray.Lenght)
 } // this while will make a randim number until it is a "new" number
 
 something = MyArray[RandomNumber]
 ConfirmationArray[RandomNumber] = true; // So it will not be picked again

If didnt understand something, goread some manual, API apges, youtube, etc..

Bye!

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

146 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 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

Spawn unique objects from the array 1 Answer

I'm trying to shuffle an array's order 3 Answers

Use all variables from array (in Random.Range()) 2 Answers

Need help with specifics in Random.Range 1 Answer

Getting the element that has the same element number on another list ? 2 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