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 SEDBC3 · Oct 22, 2013 at 08:40 PM · spawnarraysrespawnrandomize

How do I randomize the order of spawn points without repeats?

I have 30 spawn points and I need the player to visit all of them before the game is over. When the player hits one of the arrow keys, I want them to be transported to the next spawn point (it may be easier to make each of the spawn points their own level, I'm not sure). Once the player visits a spawn point, I do not want them to be able to return to that point. In other words, on the first spawn there would be 30 possibilities, on the 2nd there would be 29, etc.

I was thinking of doing this with an array and having the index destroy or delete itself after it has been visited, but if there is a better way to do it, I'm all ears.

I'm new to coding so any and all help is really really REALLY appreciated! Thank you!

Comment
Add comment · Show 1
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 rednax20 · Oct 22, 2013 at 08:54 PM 0
Share

i was about to suggest using an array of GameObjects when i read the line that said you were going to do that. Definitely what i would do, good thinking.

although there may be a better way to do it, but not any that i can think of.

1 Reply

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

Answer by Owen-Reynolds · Oct 22, 2013 at 09:15 PM

Random without repeats is sort of a special case. the best way to do it is to shuffle them ahead of time (like a deck of cards,) then visit them all in order (like once you shuffle some cards, you don't need to pick randomly -- can just deal from the top.)

A simple array shuffle (assumes that A is an array of Empties, which are Transforms):

 for(int i=0;i<A.Length;i++) {
   int i2 = Random.Range(0, A.Length); // NOTE: 0 to len-1
   // swap i and i2. OK if they are the same:
   Transform temp = A[i]; A[i]=A[i2]; A[i2]=temp;
 }

It seems like it might swap an item into the same place, or swap an item back to where it was. It can, but with the correct 1 in 30 chance.

The alternative, which seems more obvious, has you rolling, say, 0 to 24 and counting, skipping unused ones. Giant pain, and risks infinite loops if the count gets off.

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 SEDBC3 · Oct 25, 2013 at 03:15 PM 0
Share

This looks great! I'll try it out and let you know how it goes. Thanks!

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

16 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

Related Questions

Spawn Multiple Monsters 1 Answer

WaveSpawn Script. Help 0 Answers

Scripts are being disabled during game play 0 Answers

Unable To spawn Multiple Objects 1 Answer

A respawn script for enemies. 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