- Home /
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.
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.
Online converter of js (Unity) to c#: http://www.m2h.nl/files/js_to_c.php Use at own risk, if any
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;
}
}
Big thank you! This worked exactly how I needed. I've been reading up more on it and it's making more sense now.
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 :)
Solution is incorrect as methods and variables still exist outside the class. I recommend the OP looks into basic OOP practice
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;
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
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
Follow this Question
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