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 Drshk7 · Sep 17, 2013 at 02:02 AM · yieldwaitforsecondsienumeratorstartcoroutine

Fire rate for gun script, c# (not u/s)

Hello. I just started designing and testing the shooting SYSTEM first. I want my shooting to be controlled by a firerate(float) and use yield WaitForSeconds and bool 'canfire = false/true' Like this:

1st line: canfire = true; ///

2nd line: Yield WaitForSeconds blah blah ///

3rd line: canfire = true; /////////////////

So first canfire is disabled, wait seconds (firerate), then be able to fire again. Unfortunatly, C# doesn't work as simple as JavaScript. I used IEnumerator and StartCoroutine but it didn't work well. How? I had bullets--; before the yield so it made the bullet count drop to negative and all the way down and so on in a matter of seconds. Is there a way to not use coroutine? If not, can I make a different void and call it firerate and use coroutine on that? Or, how can I fix my script? Script:`using UnityEngine; using System.Collections;

public class GunScript : MonoBehaviour { public int loaded; public bool canfire; public bool reloading;

 void Update () 
 {
     if(loaded > 0 && !reloading)
         canfire = true;
     
     if(loaded == 0)
         canfire = false;
     
     if(Input.GetMouseButton(0))
         if(!reloading && canfire == true)
             StartCoroutine("fire");
 }
 
 IEnumerator fire ()
 {
     Debug.Log("Fired!");
     loaded--;
     canfire = false;
     yield return new WaitForSeconds(firerate);
     canfire = true;
     Debug.Log("Ready to fire!");
 }

} `

Don't worry about the bracket errors, the insert code here is glitchy.... Also, sorry about the bad sorting at the beginning of the script, the insert code here again did that while I was editing it........... UNITY PLEASE FIX!!!

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
0
Best Answer

Answer by Fattie · Sep 17, 2013 at 08:14 AM

It is unbelievably simple to do this using

 Invoke

and

 CancelInvoke

Just read the doco.

beginners should never use yield, or touch the run loop at all, ever, for any reason.

once you master the trivial functions Invoke and CancelInvoke - which Unity put in the engine for a reason - you can learn the advanced stuff mentioned by experts on this question.

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 SureSight · Mar 31, 2014 at 10:20 AM 0
Share

I've been looking for a better way to do timers in general within Unity. Things like enforcing fire rate and applying damage per second e.g. poison or standing in fire.

Do you have a good implementation of Invoke and CancelInvoke?

avatar image Fattie · Mar 31, 2014 at 11:34 AM 0
Share

It's unbelievably simple in Unity. Invoke, InvokeRepeating etc. are all built-in to Unity. You can find literally thousands of full examples here on this site - or anywhere on the internet. Try searching on this site on "Invoke" or "InvokeRepeating"

Eg, http://answers.unity3d.com/questions/19924/how-to-call-a-routine-every-3-seconds.html just search.

Here's a whole video, made by Unity, on the topic

http://unity3d.com/learn/tutorials/modules/beginner/scripting/invoke

Try searching on the internet: "unity3d Invoke tutorial"

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

19 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

Related Questions

WaitForSeconds does not work 0 Answers

C# WaitForSeconds doesn't seem to work ?? 2 Answers

How to make a 3d text writeOut method? 1 Answer

Scrolling Text StartCoroutine not working C# 2 Answers

WaitForSeconds problem with Unity Pro 3 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