• 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 The_Jrod · Jan 25, 2017 at 09:58 PM · randomspawnlimit

Setting a limit to how many random objects can spawn together?

First off, I just want to say I'm a beginner at this stuff so my code may not be very pretty (help if you can lol). I currently have my 5 obstacles spawning randomly in my game (think of subway surfer obstacles) and I'd like to make it so all of the obstacles cannot spawn one after another or have all 5 of them spawn right next to each other, trapping the player. At most I'd only want 3 to be able to spawn next to each other so that the other 2 obstacles can leave room for the player to avoid them. Any help is appreciated cause like I said I'm very new at this stuff and I've googled a lot and can't seem to find a direct answer.

Code (Don't laugh):

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawner : MonoBehaviour {
 
     bool Spawning = false;
     public float minTime = 0.5f;
     public float maxTime = 3.0f;
     public Vector3 spawnPoint1;
     public Vector3 spawnPoint2;
     public Vector3 spawnPoint3;
     public Vector3 spawnPoint4;
     public Vector3 spawnPoint5;
     public GameObject[] obstacles;
 
     IEnumerator SpawnObject1(int index, float seconds)
     {
         float spawnDelay = Random.Range(0f, 1f);
         while (true)
         { 
             yield return new WaitForSeconds(Random.Range(minTime, maxTime));
             Vector3 spawnPosition1 = new Vector3(spawnPoint1.x, spawnPoint1.y, spawnPoint1.z);
             Quaternion spawnRotation1 = new Quaternion();
             Instantiate(obstacles[index], spawnPosition1, spawnRotation1);
 
             yield return new WaitForSeconds(spawnDelay); 
         }
     }
 
     IEnumerator SpawnObject2(int index, float seconds)
     {
         float spawnDelay = Random.Range(0f, 1f);
         while (true)
         {
             yield return new WaitForSeconds(Random.Range(minTime, maxTime));
             Vector3 spawnPosition2 = new Vector3(spawnPoint2.x, spawnPoint2.y, spawnPoint2.z);
             Quaternion spawnRotation2 = new Quaternion();
             Instantiate(obstacles[index], spawnPosition2, spawnRotation2);
 
             yield return new WaitForSeconds(spawnDelay);
         }
     }
 
     IEnumerator SpawnObject3(int index, float seconds)
     {
         float spawnDelay = Random.Range(0f, 1f);
         while (true)
         {
             yield return new WaitForSeconds(Random.Range(minTime, maxTime));
             Vector3 spawnPosition3 = new Vector3(spawnPoint3.x, spawnPoint3.y, spawnPoint3.z);
             Quaternion spawnRotation3 = new Quaternion();
             Instantiate(obstacles[index], spawnPosition3, spawnRotation3);
 
             yield return new WaitForSeconds(spawnDelay);
         }
     }
 
     IEnumerator SpawnObject4(int index, float seconds)
     {
         float spawnDelay = Random.Range(0f, 1f);
         while (true)
         {
             yield return new WaitForSeconds(Random.Range(minTime, maxTime));
             Vector3 spawnPosition4 = new Vector3(spawnPoint4.x, spawnPoint4.y, spawnPoint4.z);
             Quaternion spawnRotation4 = new Quaternion();
             Instantiate(obstacles[index], spawnPosition4, spawnRotation4);
 
             yield return new WaitForSeconds(spawnDelay);
         }
     }
 
     IEnumerator SpawnObject5(int index, float seconds)
     {
         float spawnDelay = Random.Range(0f, 1f);
         while (true)
         {
             yield return new WaitForSeconds(Random.Range(minTime, maxTime));
             Vector3 spawnPosition5 = new Vector3(spawnPoint5.x, spawnPoint5.y, spawnPoint5.z);
             Quaternion spawnRotation5 = new Quaternion();
             Instantiate(obstacles[index], spawnPosition5, spawnRotation5);
 
             yield return new WaitForSeconds(spawnDelay);
         }
     }
 
     void Update()
     {
 
         if (!Spawning)
         {
             Spawning = true; 
             int enemyIndex = Random.Range(0, obstacles.Length);
             StartCoroutine(SpawnObject1(enemyIndex, Random.Range(minTime, maxTime)));
             StartCoroutine(SpawnObject2(enemyIndex, Random.Range(minTime, maxTime)));
             StartCoroutine(SpawnObject3(enemyIndex, Random.Range(minTime, maxTime)));
             StartCoroutine(SpawnObject4(enemyIndex, Random.Range(minTime, maxTime)));
             StartCoroutine(SpawnObject5(enemyIndex, Random.Range(minTime, maxTime)));
         }
     }
 }
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

0 Replies

· Add your reply
  • Sort: 

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

92 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

Related Questions

Spawning objects at runtime in vertical-scroller game 0 Answers

Trying to make a field of meteors forever, but I can tell I'm doing it horribly 0 Answers

Spawn enemies so they aren't instantiated on top of each other (C#) 2 Answers

Spawn gameObject horde, modify concentration of spawned objects. 1 Answer

Random Object Spawn 0 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