Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
0
Question by RoyaleNite · Mar 09, 2019 at 10:52 AM · scripting problemlistrandom.rangerandomize

Randomize a spawn position without repition

How can I randomize an x and z coordinate from 1 to 10 and then remove that x and z coordinate from the 100 possible combinations? like if i do this

  public List<int> xSpawn = new List<int>();
     public List<int> zSpawn = new List<int>();
 
     public Rigidbody fallingBlockPrefab;
     private Rigidbody fallingBlockInstance;
 
     void Start()
     {
         StartCoroutine("SpawningBlocks");
 
         for (int a = 0; a < 10; a++)
         {
             xSpawn.Add(a);
             zSpawn.Add(a);
         }
     }
 
     IEnumerator SpawningBlocks()
     {
 
         yield return new WaitForSeconds(SpawnTime);
         int randX, randZ;
         randX = Random.RandomRange(0, xSpawn.Count);
         randZ = Random.RandomRange(0, zSpawn.Count);
 
         xSpawn.RemoveAt(randX);
         zSpawn.RemoveAt(randZ);
 
         fallingBlockInstance = Instantiate(fallingBlockPrefab, new Vector3(randX, 20, randZ), transform.rotation);
         fallingBlockInstance.name = "FallingBlock " + numberSpawned;
 
         StartCoroutine("SpawningBlocks");
 
     }

but it still spawn duplicates in. So basically say once a cube has spawn at position x=2 and z=3 I don't want it to spawn at position x=2 and z=3 but still be possible to spawn at x=2 and say z=2 if it makes sense.

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

3 Replies

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

Answer by RoyaleNite · Mar 09, 2019 at 02:54 PM

I made a for loop from 10 to 110 to and then split it into x and z according to the position

 for (int a = 10; a < 110; a++)
         {
             string spawnLocation = a.ToString();
             if (spawnLocation.Length == 2)
             {
                 spawnPosition.Add(new SpawnPos((int.Parse(spawnLocation[0].ToString())), (int.Parse(spawnLocation[1].ToString()))));
             }
             if (spawnLocation.Length == 3)
             {
                 spawnPosition.Add(new SpawnPos(int.Parse(spawnLocation[0].ToString() + spawnLocation[1].ToString()), int.Parse(spawnLocation[2].ToString())));
             }
         }

then just random a number from 0 to 110 and use that number of the list then just remove it afterwards

 int randX, randZ, index;
         index = Random.RandomRange(0, 110);
 
         Debug.Log(spawnPosition[index].xPos + ";" + spawnPosition[index].zPos + "Number : " + index);
         randX = spawnPosition[index].xPos;
         randZ = spawnPosition[index].zPos;
 
         spawnPosition.RemoveAt(index);

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
1

Answer by misher · Mar 09, 2019 at 12:34 PM

  1. Create one single list with all possible positions.

  2. Shuffle the list

  3. Create a queue from the shuffled list

  4. Dequeue a position when spawning

  5. Recreate a new queue if needed

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
0

Answer by oroora6_unity · Mar 09, 2019 at 11:22 AM

You are using the index of the list for the position. Instead, you should use the value that you are removing in the coroutine.

Comment
Add comment · Show 2 · 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 RoyaleNite · Mar 09, 2019 at 11:39 AM 0
Share

What do you mean by i should use the value?

avatar image ShadyProductions RoyaleNite · Mar 09, 2019 at 12:16 PM 0
Share

he means

xSpawn.Remove(randX); ins$$anonymous$$d of RemoveAt

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

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

A node in a childnode? 1 Answer

Randomize 2 clips from an AudioSource List by name? 1 Answer

Public list are not reflecting in inspector. 1 Answer

How to make Random.Range() step by a certain amount? 1 Answer

Checking a list containing another list, and checking if the elements in the list are equal problem 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