• 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
Question by DiamondPaws · Mar 17, 2021 at 02:58 AM · spawnspawningspawning problemsspawnpointsspawner

How to prevent multiple spawns from a single spawn point?

Hey everyone,


I'm working on an endless runner style game where the player has to dodge blocks w$$anonymous$$le collecting coins. I'm using four spawn points to spawn three blocks and one coin using the code below:

 void SpawnBlocks()
     {
         int randomIndex = Random.Range(0, spawnPoints.Length);
 
         for (int i = 0; i < spawnPoints.Length; i++)
         {
             if (randomIndex != i)
             {
 
                 Instantiate(blockPrefab, spawnPoints[i].position, Quaternion.identity);
 
                 Instantiate(coinPrefab, spawnPoints[randomIndex].position, Quaternion.identity);
                
             }
 
         }
     }


Initially, I started the project by randomly spawning three blocks among four spawn points, so there would always be a gap for the player to go through. To make the game more engaging, I decided to spawn a collectable coin wit$$anonymous$$n the gap using w$$anonymous$$chever spawn point is not being used to spawn a block.


According to t$$anonymous$$s script, i = all of the spawn points that are being activated in each instance. I had to figure out w$$anonymous$$ch spawn point was not being activated so that I could use that spawn point to spawn a coin. After simply realizing that the randomIndex != i, I figured out that I could use spawnPoints[randomIndex].position to spawn the coin because the randomIndex number is the number that is never being used for the block spawn points.


T$$anonymous$$s worked flawlessly, or so I thought. When I tried implementing the score system, w$$anonymous$$ch would +1 for every coin that the player collected, I noticed that each time the player collected a coin, the score would +3 instead. That's when I realized that, instead of spawning just one coin, I was spawning three coins on top of each other.


I would like to prevent the spawn point from spawning three coins at once and, instead, just spawn one coin, but I can't wrap my head around how to do that with my current script.


I've also seen people utilize a check function to see if an object is already in the location before spawning another, w$$anonymous$$ch would work if these coins weren't spawning at the exact same time.


My last idea is to change the score value from +1 per coin to +0.3 per coin, and then round the 0.9 from the stacked coins into a 1. T$$anonymous$$s might be my best option, but I can't quite figure out how to add the decimals together and then round to a whole number (I'm basically one DNA molecule away from a c$$anonymous$$mp, after all).


Apologies for the reading assignment, I thought it'd be best to fully explain my situation to receive proper assistance. If anybody knows of a solution/workaround then I would greatly appreciate it!

Comment

People who like this

0 Show 0
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

Answer by losingisfun · Mar 17, 2021 at 03:17 AM

What you could do is create a pool and then remove the last used spawn point. Here's an example:

 public void SpawnBlocks() {
 
         // Create a pool
         List<Vector3> pool = new List<Vector3>();
         foreach (Vector3 v in spawnPoints) { pool.Add(v); }
 
         for (int i = 0; i < spawnPoints.Length-1; i++) {
             int randomIndex = Random.Range(0, pool.Count);
 
             Instantiate(blockPrefab, pool[randomIndex], Quaternion.identity);
 
             pool.RemoveAt(randomIndex);
         }
 
         // Now add the coin with the last remaining spawn point in the pool
         Instantiate(coinPrefab, pool[0], Quaternion.identity);
     }
Comment
logicandchaos
DiamondPaws

People who like this

2 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 DiamondPaws · Mar 17, 2021 at 06:05 PM 0
Share

Really fascinating method! Its implementation is above my pay grade but that just provides the motivation to read into it more. I'll check in with you if I can get this to work. I appreciate you sharing your thought process!

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

116 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

Related Questions

How to spawn all humans at once and activate then with time? 1 Answer

Random spawnpoint where only one prefab can spawn! 1 Answer

How to spawn enemies at different locations and avoid overlapping each other 2 Answers

How do I spawn more the one spawnpoints ramdomly in my scene / Spawning problems 0 Answers

[Help] How Do I Randomly Spawn Game Objects On Specific Coordinates? 3 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