Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
-1
Question by frankyboy450 · Mar 15, 2014 at 09:15 PM · c#instantiatearrayobjectrandom

Randomly instantiate objects from array without choosing the same item twice.

Hi.

How would I do if I wanted to spawn 5 random objects from an array, without the same object get's chosen twice?

I'm trying to make a "find the pair" game where you need to find the 2 objects that look the same.

I already have some code that removes all other objects spawned last time, and creates a new set of items and makes 2 of the same kind, but unfortunately sometimes there are more pairs...

Comment
Add comment · Show 3
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 Fattie · Mar 15, 2014 at 10:35 PM 0
Share

this is called "shuffling". it's an absolute basic of computer science that if you want to choose from a set with "no repeats", that's a shuffle

avatar image frankyboy450 · Mar 15, 2014 at 11:49 PM 0
Share

Oh, I see. I didn't know the word for it and had trouble finding some example for it. But if the array I'm accessing contains 100 objects and I only need 10 or so, would it also work?

avatar image Fattie · Mar 16, 2014 at 08:29 AM 0
Share

Be sure to use #List exactly as swalex explains, enjoy

2 Replies

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

Answer by Talidos · Mar 16, 2014 at 02:09 AM

I did this for an iOS game a while back. The process I ended up following was this:

  1. Create a second array you don't mind removing pieces of, and fill it with all the cards in your deck. This includes duplicates.

  2. Every time you need a new card, pick a random one from the new array and REMOVE IT AFTERWARDS.

  3. Repeat step 2 until the array is empty, and you don't need any more cards.

Sorry I don't have any code for reference, but hopefully this will still help.

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
3

Answer by swalex · Mar 16, 2014 at 01:58 AM

Don't use a plain array, but try to use a System.Collections.Generic.List instead. Fill it with your items just as you do this with your array, and simply remove the items from this list when you pick them.

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 Fattie · Mar 16, 2014 at 08:30 AM 0
Share

Exactly, OP learn about List -- see many questions here

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

24 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

Related Questions

Unable to randomly instantiate prefab from array 2 Answers

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

Random array issue C# 2 Answers

Random Children Array that changes Parent, not working right 0 Answers

Randoim values in a dynamic array C# 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