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
3
Question by vincentamato · Dec 08, 2014 at 01:17 AM · gameobjectinvokerepeating

Increasing Spawn Rate as Game Goes On

Hi. In my game I have a player climbing up a wall and objects fall on the player. (Credits to @static_cast). I was wondering how I can increase the spawn rate of the object every 30 seconds. In other word to make more objects fall as the game goes on. How should I do this?

Comment
Add comment · Show 2
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 vincentamato · Dec 08, 2014 at 12:30 AM 0
Share

@$$anonymous$$rSoad I like that idea but I only would like to increase the spawn rate. I am a beginner intermediate coder so I am not so sure you to make a difficulty script. Is there anyway I can just increase the spawn rate every 30 seconds?

avatar image vincentamato · Dec 08, 2014 at 12:59 AM 0
Share

@$$anonymous$$rSoad I have two questions. One how do I set a limit. And two I'm not sure what the last sentence of your comment really means. How do I restart my spawn function?

3 Replies

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

Answer by MrSoad · Dec 08, 2014 at 01:18 AM

Hi again, take a look at this code. I have not ran this so maybe there is a typo somewhere :

 private var fSpawn_Speed : float;
 
 function Start() {
 
     //Set initial Spawn Speed
     fSpawn_Speed = 10f;
     
     //Start Spawning.
     InvokeRepeating("subSpawn_Object", 0, fSpawn_Speed);
     
     //Start the Spawn speed adjust in 30 seconds.
     InvokeRepeating("subIncrease_Spawn_Speed", 30, 30);
 }
 
 function subIncrease_Spawn_Speed() {
 
     var fSpeed_Increase : float = 1f;
     
     //Cancel the current subSpawn_Object Invoke.
     CancelInvoke("subSpawn_Object");
 
     //This will limit the spawn speed to a min of 1.
     if ((fSpawn_Speed - fSpeed_Increase) < 1) {
         fSpawn_Speed = 1f;
     } else {
         fSpawn_Speed = fSpawn_Speed - fSpeed_Increase;
     }
     
     //Restart subSpawn_Object with new repeat time.
     //You may want to adjust for the time since the last spawn
     //by setting up a time since last spawn var and using that
     //calculation rather than 0.
     InvokeRepeating("subSpawn_Object", 0, fSpawn_Speed);
 }
 
 function subSpawn_Object() {
     //Your Spawn code.
 }
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 static_cast · Dec 08, 2014 at 01:26 AM 0
Share

Couldn't you just use Time.time and nextSpawn to change the spawn times?

avatar image vincentamato · Dec 08, 2014 at 02:48 AM 0
Share

@$$anonymous$$rSoad This is great except the transition is not smooth. It starts at one and then after 45 seconds (Which is what I want it to be ins$$anonymous$$d of 30) it goes to 0.5 and then after another 45 seconds it goes to 0.3. Is there anyway to have a smooth transition, so that it goes like 1 down to 0.9 to 0.8 to 0.7... etc.?

avatar image static_cast vincentamato · Dec 08, 2014 at 04:34 AM 0
Share

Did you try my solution?

It's possibly not smooth because you need to change the fSpeed_Increase variable. ;)

avatar image vincentamato vincentamato · Dec 08, 2014 at 11:59 AM 0
Share

Thank you so much @static_cast and @$$anonymous$$rSoad! You guys have helped me so much! @static_cast I tried to try your solution but I didn't know where to put my instantiate code in. Anyways I will tag you guys if I need any more help with this topic.

avatar image static_cast vincentamato · Dec 08, 2014 at 04:17 PM 0
Share

You just replace your code with it.

Basically, delete your original Update() and replace it with $$anonymous$$e. ;)

Show more comments
avatar image
0

Answer by static_cast · Dec 08, 2014 at 01:29 AM

Here's what you could do:

 private float nextDrop = 0f; //The time for the next item to spawn
 private float dropInterval = 10f; //The interval between spawned items
 private float changeInterval = 30f; //The interval between changing the interval between spanwed times. :P
 
 void Update()
 {
     if(Time.time >= nextDrop) //If ready to spawn
     {
         SpawnObject();
         nextDrop += dropInterval; //Set next spawn time

         if(Time.time >= changeInterval) //If ready to change spawn interval
         {
             if(dropInterval > 1f) //Change spawn interval to 3/4ths what it was
                 dropInterval *= 0.75f;
             else //Make sure dropInterval stays above 1.
                 dropInterval = 1f;
         }
     }
 }

Comment
Add comment · Show 11 · 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 static_cast · Dec 08, 2014 at 01:46 AM 0
Share

To me, working code is not the same as organized code. You can put code into 10 nested coroutines, or you could put it where it counts.

Why not put it in Update()? From what I've seen of this guy's code, this is his spawning script. Here's what http://forum.unity3d.com/threads/invokerepeating-vs-update-battle-of-efficiency.214901/ has to say about it: "That isn't to say, however, that there isn't a lot more overhead required to call your "Invoke" method through InvokeRepeating. It's doing this calling through reflection, which is slow. (see: $$anonymous$$ethodInfo.Invoke $$anonymous$$ethod)"

avatar image vincentamato · Dec 08, 2014 at 01:57 AM 0
Share

@static_cast I like your idea but where would I put my spawn code?

avatar image static_cast · Dec 08, 2014 at 01:58 AM 0
Share

True the big long update, but I don't see what could possible be wrong with the script. It works, it's efficient, and it's the purpose of said script.

I wouldn't put walking scripts in a script used to spawn objects, would I?

avatar image vincentamato · Dec 08, 2014 at 01:59 AM 0
Share

@$$anonymous$$rSoad I code in C# so I translated your code into C# and the only error I'm getting is that the fSpeed_Increase variable was never declared.

avatar image MrSoad · Dec 08, 2014 at 02:09 AM 0
Share

Do you know how to fix that Vinnya124 ?

Just incase you don't replace :

 var fSpeed_Increase : float = 1f;

with :

 float fSpeed_Increase = 1f;
Show more comments
avatar image
0

Answer by martin101 · Dec 12, 2014 at 09:54 AM

Download this free asset in which there are 4 games. One of the games uses Spawn function which is what you need.

C# game examples by M2H

https://www.assetstore.unity3d.com/#!/content/116 Play the Egg bucket Game ( thats what you need )

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Updating then calling gameObject from main thread. error 0 Answers

InvokeRepeating 1 Answer

If you disable a gameobject, does an InvokeRepeating loop end or pause? 3 Answers

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Game freezes everything when calling WaitForSeconds 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