Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 richrow · Sep 01, 2018 at 11:35 PM · instantiaterandomgameobjectsinvokerepeatingsequence

How to Spawn multiple game object one by one in random order

i am creating a game where 3 random game objects ( green cube, red cube and blue cube) spawn one by one in random order. my approach to this is creating a RNG value for each game object ("if RNG <= 5 redcube will spawn") but that causes the spawn to either ( just to know each cube has a limited number to spawn like redCube=10 GreenCube=3 BlueCube=7):

1-overlap with another object.

2- spawn all 3 at once

3- take too long to spawn cause of RNG.

i have been stuck on this issue for months and i dont know what to do anymore, it is a core feature in my game and it would be nice if someone experienced could help me with my issue.(i am using invokerepeat for the spawning).

i want the game objects to spawn even if one of them have finished spawning. E.g redcube gameobject is 0 but greenobject and blueobject should still spawn till all have finished.

its not an endless game.

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 TreyH · Sep 06, 2018 at 07:37 PM 0
Share

To confirm, you'd like to configure some number of colored cubes to spawn in a randomized order?

Are these on independent timers or do you expect them to all happen in evenly spaced out intervals?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dspruill · Sep 02, 2018 at 01:39 AM

Can you give me a little bit more to go on about how exactly you are spawning?

For instance, I suggest having a single script controlling the spawning of all 3 from there, I recommend a for loop (please excuse the psuedo code)

 int numRed = 10
 int numBlue = 7
 int numGreen = 3
 
 //spawns 10 objs
 int rand
 for(/*total spawn amount*/) {
 rand = Random.Range(0, numRed + numBlue + numGreen);
 
 if (rand< numRed){
 //spawn Red
 }
 else if (rand < numRed + numBlue){
 //spawn blue
 }
 
 //...etc...
 
 }

This should at least give you a start on the problem :). Let me know what else you need

Comment
Add comment · Show 7 · 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 richrow · Sep 06, 2018 at 01:41 AM 0
Share

@dspruill this is my current spawn code:

     private int FirstBubbleLimit = 0;
 private int SecondeBubbleLimit = 0;
 
 private int FirstBubbleRNG;
 private int SecondBubbleRNG;
 

 
 

 void Start () 
 {   InvokeRepeating("SpawnFirstBubble", 3f, 3f);
     InvokeRepeating("SpawnSecondBubble", 5f, 2f);
 }

   
 
 void SpawnFirstBubble () 
 {
     
 FirstBubbleRNG  = Random.Range (1, 100);
     
     if (FirstBubbleRNG <= 50) 
        {
         Vector3 spawnPosition = new Vector3 (Random.Range (-2.3f, 2.3f), 6, 5);
         Quaternion spawnRotation = Quaternion.identity;
         Instantiate (Bubbles[0], spawnPosition, spawnRotation);
         FirstBubbleLimit++;                                          
         if (FirstBubbleLimit == 5)                                   
         {
             CancelInvoke ("SpawnFirstBubble");                
         }
         }
 }
 
 
 void SpawnSecondBubble ()
 {
     SecondBubbleRNG = Random.Range (1, 100);
     if (SecondBubbleRNG <= 100) 
        {
         Vector3 spawnPosition = new Vector3 (Random.Range (-2.3f, 2.3f), 6, 5);
         Quaternion spawnRotation = Quaternion.identity;
         Instantiate (Bubbles[1], spawnPosition, spawnRotation);
          SecondeBubbleLimit++;
          if (SecondeBubbleLimit == 10) 
         {
             CancelInvoke ("SpawnSecondBubble");
         } 
        }
     
 }

the intention for this code is to spawn 2 or more game objects till a certain amount $$anonymous$$g 10 redobjects. while the blueobjects will keep spawning till it runs out too. but the problem is that its too random i wonder if there is a better way to control the way it spawns

avatar image dspruill richrow · Sep 06, 2018 at 02:13 AM 0
Share

I'm sorry, but I still don't entirely get what you're trying to do. I also just realized that I left out a part in my pseudo code above. At the end of each if statement, decrement the num color. For ex:

 if (rand< numRed){
  //spawn Red
 
 numRed--;
  }
avatar image richrow dspruill · Sep 06, 2018 at 06:20 PM 0
Share

thank for replying and taking your time to help me.

what i am trying to do is to :

spawn 3 game objects, in a random order ( so if player restarts the level it would be different every time) but they have to be a limited number of game objects so that the game can end. it isn't an endless game.

The problem: with my code it spawns the game objects in random order, but because i am using three different Random Values at the same time for each game object. Sometimes it spawn at the same time or take too long to spawn because of random numbers.

i prefer the game object to spawn 1 by 1 and stop after a certain amount.

ty

i have seen the code you provided, the only part i dont understand is :

 //spawns 10 objs
  int rand
  for(/*total spawn amount*/) {
  rand = Random.Range(0, numRed + numBlue + numGreen);
 
Show more comments

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

115 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

Related Questions

Spawn GameObjects without overlap 1 Answer

Invoke is not working properly. 0 Answers

Assign a random material to objects 1 Answer

Problem with Random.range 1 Answer

Instantiate random from a list gives error: "Reference not set to an instance" 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