Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 CubePhysics · Jul 08, 2015 at 06:47 PM · instantiatetimewaitforsecondsmethod

WaitForSeconds() Is not working.

In my code whenever I click a prefab is instantiated (shooting). But i want a cool down town timer so i added the WaitForSeconds method but its not working. What is the problem. Here is my code:

     void Update () {
         if(Input.GetMouseButton(0))
         {
         StartCoroutine( Shoot());
         }
     }
 
     IEnumerator Shoot()
     {
         Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
         yield return new WaitForSeconds(5.0f);
     }
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

5 Replies

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

Answer by DiegoSLTS · Jul 08, 2015 at 07:10 PM

Coroutines are not used for that, they don't prevent a piece of code from being called, you're starting a new Shoot coroutine on every Update where the condition is true (the mouse button being down).

A coroutine is just a method like every other, but it can stop at some point and continue from there later. In your case your coroutine is instantiating the laser, then stops for 5 seconds and then it finishes.

I'd do it without a coroutine, just store the time of the previous shoot and check if it passed enough time in the if condition. Something like this:

 public float lastShootAt;
  
 void Update () {
     if(Input.GetMouseButton(0) && Time.time >= lastShootAt + 5f)
     {
         lastShootAt = Time.time;
         Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
     }
 }
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 CubePhysics · Jul 09, 2015 at 07:06 PM 0
Share

Brilliant :D. Thanks it did just the job!

avatar image
2

Answer by sillyjake · Jul 08, 2015 at 09:56 PM

Here, Try this:

 public bool canShoot = true;
 void Update () {
   if(Input.GetMouseButton(0) && canShoot) {
     StartCoroutine( Shoot());
   }

  IEnumerator Shoot() {
    canShoot = false;
    Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
    yield return new WaitForSeconds(5.0f);
    canShoot = true;
  }
 }


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
1

Answer by Daphoeno · Jul 08, 2015 at 07:00 PM

have you tried adding a boolean to activate and deactivate the cooldown either end of the IEnumeration to prevent it being called again? just adding a WaitForSeconds will not prevent the Coroutine from being called again....

Try this...

 public bool _cooldown = false;
 
 void Update () {
          if(Input.GetMouseButton(0) && _cooldown == false)
          {
          StartCoroutine( Shoot());
          }
      }
  
      IEnumerator Shoot()
      {
          _cooldown = true;
          Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
          yield return new WaitForSeconds(5.0f);
          _cooldown = false;
      }
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 DiegoSLTS · Jul 08, 2015 at 07:23 PM 1
Share

This works, but I think the Shoot coroutine does more than Shooting. Something like this might be clearer:

  public bool _cooldown = false;
  
  void Update () {
           if(Input.Get$$anonymous$$ouseButton(0) && _cooldown == false)
           {
               Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);
               StartCoroutine(Cooldown());
           }
       }
   
       IEnumerator Cooldown()
       {
           _cooldown = true;
           yield return new WaitForSeconds(5.0f);
           _cooldown = false;
       }
avatar image
-2

Answer by ei1 · Jul 08, 2015 at 09:57 PM

I don't think you need to say StartCoroutine. Try just saying

Shoot();

instead of

StartCoroutine(Shoot());

Also try saying just

yield WaitForSeconds(5.0f);

if that doesn't work, I would switch to javascript. I switched from c# to javascript and its much faster.

function Update () {

      if(Input.GetMouseButton(0))  

      {  

      Shoot();  

      }  

  }  

   

  function Shoot()  

  {  

      Instantiate(Laser, Launcher.transform.position, Launcher.transform.rotation);  

      yield WaitForSeconds(5);  

  }  
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 DiegoSLTS · Jul 08, 2015 at 10:53 PM 0
Share

First, your suggestions and the code you posted have the same problems that the OP is trying to solve. Anyway, in C# you need the "StartCoroutine" bit, and you need the complete "yield return new Wait..." line. Those things you suggested only work with UnityScript.

Also, UnityScript might be faster to write (you can ignore types and some keywords) but as for the actual execution speed using one language or the other, it depends on the generated code. It's usually the same speed, but UnityScript can be slower. Look here: http://answers.unity3d.com/questions/7567/is-there-a-performance-difference-between-unitys-j.html

So, switching to UnityScript because some code doesn't work is a bad suggestion.

avatar image
0

Answer by underscorefrog · Jul 09, 2015 at 06:56 AM

try removing the 0 in 5.0f.

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

25 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

Related Questions

how to spawn a object once (timed) 1 Answer

Instantiate objects faster and faster over game time,Instantiate objects faster and faster over time 1 Answer

Using WaitForSeconds and trigger 1 Answer

Instantiate 1 at a time at each transform in array 1 Answer

Timed instantiantion not quite working 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