Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by Sillan · Feb 13, 2013 at 05:34 PM · timespawnsecondsspawn points

Spawn after every 5 seconds

Hey Guys, Johnny here :D I was just wondering if anyone knew how to spawn a gameobject after every 5 seconds. I'm creating a survival game where an enemy is spawned every 5 seconds. Is anyone able to help me out? Thanks so much :D

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

4 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by pcorazao · Mar 31, 2015 at 10:30 PM

public class BallHopperController : MonoBehaviour {

 public GameObject ball;
 public float spawnTime = 3f;

 // Use this for initialization
 void Start () {
     InvokeRepeating ("SpawnBall", spawnTime, spawnTime);
 }
 
 // Update is called once per frame
 void Update () {

 }    

 void SpawnBall()
 {
     var newBall = GameObject.Instantiate(ball);
 }

}

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

Answer by Dave-Carlile · Feb 13, 2013 at 05:35 PM

You can use Invoke to execute a function every 5 seconds. In that function Instantiate your game object.

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 insominx · Feb 13, 2013 at 05:37 PM 1
Share

Specifically, you might wanna use the repeating version: InvokeRepeating.

avatar image Sillan · Feb 13, 2013 at 11:58 PM 0
Share

Thankyou I'll try it :D

avatar image
1

Answer by Razbijacs · Aug 21, 2019 at 05:56 PM

Time.time is counting time from the beginning of Play, i is checkpoint for Time.time to reach. As the Time.time reaches checkpoint,code is executed and the checkpoint is now moved for extra "i" seconds.I hope this helps all of you searching for the answer.

 int i; // i=0.
 public GameObject Enemy;
 void Update()
     {
 
         if (Time.time> i)
         {
             i += 2;
             Instantiate(Enemy,gameObject.transform);
         }
     }

, Time.time counts the real time seconds, and can only be red. "i" sets the check point for Time.time to reach,and when it reaches it code is executed and checkpoint is moved for additional "i" seconds.I hope this helps you all that are searching for a solution.

     int i;
     public GameObject Enemy;
     // i==0;
      void Update()
         {
             if (Time.time> i)
             {
                 i += 5;
                 Instantiate(Enemy,gameObject.Transform.position);
             }
         }

 

 




Comment
Add comment · Show 1 · 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 tenzinnorbubg · Jul 04, 2021 at 03:43 AM 0
Share

float waitFor = 5f; waitFor -= time.time; if(waitFor >= 0) { //TODO waitFor = 5f; } // what is happening here?? i am sorry it might be stupid Q but i expect same result of your code from here, didn't work

avatar image
0

Answer by SkillcraftHD · Feb 25, 2021 at 04:18 PM

Use an IEnumerator function with yield return new WaitForSeconds(5f) in it. Call it when you want it to first be spawned and also at the end of the IEunermator function.

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

15 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

Related Questions

Instantiating object after some fixed time... 2 Answers

How to change the colour of a light every few seconds. 1 Answer

Creating function that lasts for x seconds 3 Answers

Loading A Level After 40 Seconds 2 Answers

Randomize Spawn Time 2 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