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 /
  • Help Room /
avatar image
0
Question by Ejpj123 · Jul 16, 2016 at 02:06 AM · script.timeobjectsstopfrequency

How would I create a script that spawns objects more frequently as time goes on, then stops after a certain point/amount of time?

For my game, I have an enemy that works perfectly. Now how would I make a mode, in which the enemy spawns more frequently as time goes on, then the spawn rate just stays the same the whole time. The reason I am asking this is because I have a script for my camera to where it gets faster and faster than stays at the same speed for the rest of the game. I have no idea where to start with a script like this, so any help would be appreciated. Here is the spawn enemy script.

 using UnityEngine;
 using System.Collections;
 
 public class SpawnEnemy : MonoBehaviour {
 
     public GameObject[] obj;
     public float spawnMin = 1f;
     public float spawnMax = 2f;
 
     // Use this for initialization
     void Start () {
         Spawn ();
     
     }
     
     // Update is called once per frame
     void Spawn () {
         Instantiate (obj [Random.Range (0, obj.GetLength (0))], transform.position, Quaternion.identity);
         Invoke ("Spawn", Random.Range (spawnMin, spawnMax));
     
     }
 }
 

Thanks for helping!

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

1 Reply

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

Answer by LuckierBread · Jul 16, 2016 at 05:55 AM

I'm no pro, But I would use a coroutine a for loop and a while loop. I wrote some code below go ahead and see if it satisfies your needs just remember that the spots I left for you to fill in are floats.

Also make sure you select this answer if it helped.

 void Start()
 {
     StartCoroutine(SpawnSpeed());
 }

 IEnumerator SpawnSpeed()
 {
     for (float Time = ("Time between first and second spawn");
          Time > ("min time between spawns"); 
          Time -= ("how much time you want to cut between spawns") )
     {
         Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
         yield return new WaitForSeconds (Time);
     }
     while(true)
     {
         Instantiate(obj[Random.Range(0, obj.GetLength(0))], transform.position, Quaternion.identity);
         yield return new WaitForSeconds("min time between spawns");
     }
 }


Comment
Add comment · Show 16 · 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 Ejpj123 · Jul 17, 2016 at 12:28 AM 0
Share

@LuckierBread Thank you so much! However, I need the objects to spawn randomly within a specific timeframe, except not spawning too close to each other at the same time. $$anonymous$$g., the objects spawn between 2 and 4 seconds without getting too close to each other. Also, I want the player to either have to jump over one object at a time or two objects if the objects are close together.

avatar image Ejpj123 Ejpj123 · Jul 19, 2016 at 03:24 AM 0
Share

Can someone help on the comment I said?

avatar image DonkeyMalonkey Ejpj123 · Jul 19, 2016 at 06:34 AM 0
Share

You would propably just have to throw a Random.Range(x,y) in there. You can use it to let the bot spawn in a random location (int recommended, because it gives more variation. Float could be to close together). For the spawn time I would just have the decreasing spawn time + a Random.Range(2,4). The spawn time will decrease while they will still spawn randomly.

Hope this helps!

Show more comments
avatar image Ejpj123 · Jul 20, 2016 at 04:23 PM 0
Share

@LuckierBread How would I add the lines of code in the script to what @Donkey$$anonymous$$alonkey said?

avatar image Ejpj123 Ejpj123 · Jul 22, 2016 at 10:42 PM 0
Share

Can someone help please?

avatar image Ejpj123 Ejpj123 · Jul 24, 2016 at 11:14 PM 0
Share

HE L L O ?

Show more comments
avatar image Ejpj123 · Jul 30, 2016 at 12:17 AM 0
Share

@LuckierBread Thank you so much! But i'm just not sure where to put the line of code. Could you show where to put it?

avatar image Ejpj123 · Jul 30, 2016 at 10:35 PM 0
Share

I didn't mean position. I meant spawning like something between 2 and 4 seconds. @LuckierBread

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

64 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

Related Questions

How to make my powerup last for about five seconds then go back to normal? 3 Answers

Make an Object Stop Moving Upon Collision 0 Answers

Lap Timer // c# Help 0 Answers

Stop velocity on Keydown for certain time 1 Answer

Why Trail Renderer and A Basic Script Stops Working? 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