- Home /
how to yield in C#
Okay I would like to do is make a swinging sword swinging script and limit how fast you can do this. I know how to use yield in java but I'me having trouble translating to c#. I've tried to use bool
IEnumerator Up() {
canswing=false;
yield return new WaitForSeconds(3);
canswing=true;
}
that gave me errors so I separated true and false with two different functions I have no way of testing that out because Its not even calling on the IEnumerator function how would I call upon it through Update at the click of a button
this is my current script
public bool canswing = true;
IEnumerator Up() {
haswung();
yield return new WaitForSeconds(3);
Return();
}
void Update () {
if(canswing){
if(Input.GetMouseButtonDown(0)){
animation.Play("swing");
Up();
}}
}
void haswung(){
canswing=false;
}
void Return(){
canswing=true;
}
Answer by aldonaletto · Jan 27, 2013 at 12:53 AM
In C# you must call a coroutine with StartCoroutine:
void Update() {
if(canswing){
if(Input.GetMouseButtonDown(0)){
animation.Play("swing");
StartCoroutine(Up());
}
}
}
bool canswing = true;
IEnumerator Up() {
canswing=false;
yield return new WaitForSeconds(3);
canswing=true;
}
Another hint: in JS you simply use yield SomeCoroutine(); to chain a coroutine to another one, but in C# you must use yield return StartCoroutine(SomeCoroutine());
Your answer
Follow this Question
Related Questions
IEnumerator's did not read "bool" after yield return new WaitForSeconds. 1 Answer
Multiple Cars not working 1 Answer
sprint timer 1 Answer
Distribute terrain in zones 3 Answers
Proper Way to Wait in C# 1 Answer