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 steelesound · Oct 20, 2014 at 12:08 AM · c#javascriptcooldown

Adding a Cooldown to a C# script

I have several scripts I use that have cooldowns on them, I now have a script in C# that I need to behave in the same way but I'm having trouble adding the cooldown to my script:

Here is an example of my working script:

 #pragma strict
  
 var isPlaying = false;
 var coolDown = 1.5;
 
 
 
 function Update() {
     if (!isPlaying && Input.GetButtonDown("CreepingDeath")) {
         PlayPS();
     }
 }
  
 function PlayPS() {
     isPlaying = true;
     particleSystem.Play();
     yield WaitForSeconds(6.0);
     particleSystem.Stop();
     yield WaitForSeconds(coolDown);
     isPlaying = false;
 }



Here is what I've come up with for my C# script, but I just can't get it to work:

 using UnityEngine;
 using System;
 using System.Collections;
 
 
 bool isPlaying = false;
 bool coolDown = 1.5f;
 
 
 public class TriggerWwiseEvent : MonoBehaviour {
     void Update() {
         if (!isPlaying && Input.GetButtonDown("CreepingDeath")){
             PlayPS();
             
 
         
     }
 }
 
 
 
     void PlayPS() {
     isPlaying = true;
     GetComponent<AkTriggerOnKeyPress>().Keypress();
     yield return new WaitForSeconds(coolDown);
         isPlaying = false;
     }


I'm way more comfortable in Js so any help would be very much appreciated here.

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 Habitablaba · Oct 20, 2014 at 01:51 AM 0
Share

You should do a search for how to use coroutines in c#/unity, since that is what you are trying to do. There is plenty of information out there.

avatar image richyrich · Oct 20, 2014 at 02:08 AM 0
Share

Online converter of js (Unity) to c#: http://www.m2h.nl/files/js_to_c.php Use at own risk, if any

2 Replies

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

Answer by LeftRight92 · Oct 20, 2014 at 04:22 AM

There are a few things wrong with you C# conversion. I recommend you look into the basics of OOP and bear in mind that ALL methods and variables MUST belong to a class.

Not sure whether you wanted you PlayPS() method to more closely resemble the one in the js script, I've created a second method called PlayPSAlt() which follows the same structure as the js version. The normal version mirrors the one in your attempted C# conversion.

This solution makes use of Coroutines, which you may want to read into.

 using UnityEngine;
 using System;
 using System.Collections;
 
 public class TriggerWwiseEvent : MonoBehaviour {
     private float coolDown = 1.5f;
     private bool isPlaying = false;
     
     void Update() {
         if (isPlaying && Input.GetButtonDown("CreepingDeath")){
             StartCoroutine("PlayPS");
         }
     }
 
     IEnumerator PlayPS() {
         GetComponent<AkTriggerOnKeyPress>().Keypress();
         isPlaying = true;
         yield return new WaitForSeconds(coolDown);
         isPlaying = false;
     }
 
     IEnumerator PlayPSAlt()
         //Closer to function in original javascript
         particleSystem.Play();
         isPlaying = true;
         yield return new WaitForSeconds(6.0f);
         particleSystem.Stop();
         yield return new WaitForSeconds(coolDown);
         isPlaying = 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 steelesound · Oct 20, 2014 at 05:15 AM 0
Share

Big thank you! This worked exactly how I needed. I've been reading up more on it and it's making more sense now.

avatar image
0

Answer by Turkeybag · Oct 20, 2014 at 03:13 AM

  using UnityEngine;
  using System;
  using System.Collections;
  
  public class TriggerWwiseEvent : MonoBehaviour {

  bool isPlaying = false;
  bool coolDown = 1.5f;
  
      void Update() {
          if (!isPlaying && Input.GetButtonDown("CreepingDeath")){
              StartCoroutine(PlayPS());
              
  
          
      }
  }
  
  
  
      IEnumerator PlayPS() {
      isPlaying = true;
      GetComponent<AkTriggerOnKeyPress>().Keypress();
      yield return new WaitForSeconds(coolDown);
          isPlaying = false;
      }


With C# you need to use StartCoroutine and IEnumerator when creating functions that use yield. The above script should fix your problem and show you how it all works :)

Comment
Add comment · Show 4 · 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 LeftRight92 · Oct 20, 2014 at 03:17 AM 0
Share

Solution is incorrect as methods and variables still exist outside the class. I recommend the OP looks into basic OOP practice

avatar image steelesound · Oct 20, 2014 at 03:28 AM 0
Share

Yeah this didn't work, I'm getting CS0116 "A namespace can only contain types and namespace declarations" In regards to

 bool isPlaying = false;
   bool coolDown = 1.5f;

avatar image LeftRight92 · Oct 20, 2014 at 03:39 AM 0
Share

I have posted a solution, but it is parked for moderating. Namely, the issue above is that in OOP languages, all methods and variables need to exist inside the class, not outside it. If you move the two bool declarations and PlayPS() inside of TriggerWwiseEvent (but not inside Update()) it should work

avatar image Turkeybag · Oct 20, 2014 at 04:26 AM 0
Share

Whoops, was only looking at the way you'd set up your functions rather than the variables. Edited it and it should work now.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Problem Javascript to C# 3 Answers

dealing with gameObject formations 1 Answer

Animated camera problems 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