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
0
Question by Federico_C · May 14, 2021 at 11:32 PM · spawningfaster

Trying to increase spawnspeed every milisecond/frame, please Help!

Hello, I am kind of a beginer in Unity and I have the following problem: I am trying to make that every FRAME my spawner spaws faster. Now, I realized that my code doesn´t have a variable with the speed that spawns, no, that would be simple, but instead I have a timer that goes down, spawns a prefabs , resets the timer and deletes the clone of the prefab.

If you can add a variable, a while code or anything that can help, or a link to a post and stuff like that, I would really appreciate that. Here is my code:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class Spawn : MonoBehaviour {

 public GameObject bulletPrefab;
 GameObject bulletPrefabClone;
     public Transform spawnPoint;
 public float Timer = 2f;
 public GameObject badPrefab;
 GameObject badPrefabClone;



void Start() {

}

void Update() {

  Timer -= Time.deltaTime;
  if (Timer <= 0f)
  {
      bulletPrefabClone = Instantiate(bulletPrefab, new Vector3(Random.Range(-9, 9), 5f, 0f), transform.rotation) as GameObject;
      Timer = 2f;
 Destroy(bulletPrefabClone, 2.1f);



badPrefabClone = Instantiate(badPrefab, new Vector3(Random.Range(-9, 9), 5f, 0f), transform.rotation) as GameObject; Timer = 2f; Destroy(badPrefabClone, 2.1f);

  }

}

}

NOTE: English isn´t my native language.

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 Isaac_Medina225 · May 15, 2021 at 12:15 AM 0
Share

May I ask if you have a code for your bullet and bad prefab?

avatar image logicandchaos · May 19, 2021 at 02:31 PM 0
Share

You should add in object pooling instead of instantiating and destroying, not sure why no one mentioned that in their answers.. Also are you just making a new enemy and then immediately destroying it?

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ytsorf · May 15, 2021 at 12:39 AM

Hellor there you have multiple ways to do that but... you will kill your pc xD try this code

 IEnumerator Spawner()
     {
         if(Timer <= 01f){
         StopAllCourutines();//i dont remember if is like this xD
 }
         else{
         Timer -= 0.1f; // put the var in timer pls
         bulletPrefabClone = Instantiate(bulletPrefab, new Vector3(Random.Range(-9, 9), 5f, 0f), transform.rotation) as GameObject;
         Destroy(Timer - 0.1f);
         yield return WaitForSecondsRealtime(Timer);
         Spawner();
      }
        
 
  }

Sorry for my code, i dont like to write without the Lanes and spaces xD if you dont want the ienumerator to stop, take out the stopallcourutines and use a diff way to use the timer or put a limit hope it helps <3 btw: Make the call of the spawner in the start DO NOT PUT THE SPAWNER() IN THE UPDATE OR FIXEDUPDATE you will crash ur unity

Comment
Add comment · Show 2 · 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 Isaac_Medina225 · May 16, 2021 at 10:00 AM 0
Share

He can still put the Spawner() on the UPDATE or FIXEDUPDATE as long as he limits the spawn number :)

avatar image Isaac_Medina225 · May 16, 2021 at 10:02 AM 0
Share

For example, he can have a variable that will count the number of spawned objects and stop spawning once the maximum spawn has been reached.

avatar image
0

Answer by Isaac_Medina225 · May 15, 2021 at 01:38 AM

I hope this can solve your problem:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Spawn : MonoBehaviour
 {
     public Transform spawnPoint;
     public float Timer = 2f;
 
     public GameObject bulletPrefab;
     public GameObject badPrefab;
 
     GameObject bulletPrefabClone;
     GameObject badPrefabClone;
 
     private float speedIncreaser = 0.05f; // Change this to change the spawn speed
     private float acceleration = 1.3f; // Change this to change the acceleration
 
     void Update()
     {
         Timer -= Time.deltaTime;
 
         if (Timer <= 0f)
         {
             bulletPrefabClone = Instantiate(bulletPrefab, new Vector3(Random.Range(-9, 9), 0f, 0f), transform.rotation) as GameObject;
             badPrefabClone = Instantiate(badPrefab, new Vector3(Random.Range(-9, 9), 0f, 0f), transform.rotation) as GameObject;
 
             if ((speedIncreaser * acceleration) <= 2) // this will make sure that your  game wont crash due to spawnspeed. I use 2 because your timer is 2f
             {
                 speedIncreaser *= acceleration;
             }
 
             Timer = 2f - speedIncreaser;
 
             Destroy(bulletPrefabClone, Timer);
             Destroy(badPrefabClone, Timer);
         }
     }
 }


I made modify a few modification on your code, I remove the unnecessary lines.

Comment
Add comment · Show 2 · 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 Federico_C · May 16, 2021 at 06:03 PM 0
Share

Everything is working as I wanted ! I am so happy and I wanted to thank you. I´ve seen people posting their code so the post isn´t useless, so here it goes:

 using System.Collections;
  using System.Collections.Generic;
  using UnityEngine;
  
  public class newSpawn : MonoBehaviour
  {
      public Transform spawnPoint;
      public float Timer = 2f;
  
      public GameObject bulletPrefab;
      public GameObject badPrefab;
  
      GameObject bulletPrefabClone;
      GameObject badPrefabClone;
  
      private float speedIncreaser = 0.05f; // Change this to change the spawn speed
      private float acceleration = 1.3f; // Change this to change the acceleration
  
      void Update()
      {
          Timer -= Time.deltaTime;
  
          if (Timer <= 0f)
          {
              bulletPrefabClone = Instantiate(bulletPrefab, new Vector3(Random.Range(-9, 9), 9f, 9f), transform.rotation) as GameObject;
              badPrefabClone = Instantiate(badPrefab, new Vector3(Random.Range(-9, 9), 9f, 9f), transform.rotation) as GameObject;
  
              if ((speedIncreaser * acceleration) <= 2) // this will make sure that your  game wont crash due to spawnspeed. I use 2 because your timer is 2f
              {
                  speedIncreaser *= acceleration;
              }
  
              Timer = 2f - speedIncreaser;
  
              Destroy(bulletPrefabClone, Timer);
              Destroy(badPrefabClone, Timer);
          }
      }
  }
 
 
 
avatar image Isaac_Medina225 · May 19, 2021 at 11:51 AM 0
Share

You're most welcome, you can upvote my answer to flag it as a correct answer :)

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

122 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

What is the most efficient way to spawn objects with probability weights in this scenario? 2 Answers

Trying to move the instantiated position 1 Answer

prevent object from falling to the left side when instantiated (spawned) ? 1 Answer

How to make enemies spawn IN the ground without rising up automatically first? 1 Answer

How faster load texture from bytes? (Android) 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