- Home /
Getting a "type expected" error on WaitForSeconds
Monodevelop is telling me "type expected" between the ( and 3 on the WaitForSeconds of the code.
public class dimensionswitcher : MonoBehaviour {
IEnumerator Wait();
yield WaitForSeconds(3);
bool dimension1 = true;
bool dimension2 = false;
void Update () {
//if player hits z they switch to dim2 and cant switch for 3 sec
if (Input.GetKeyDown ("z"))
if (dimension1 == true)
dimension2 = true;
dimension1 = false;
transform.position = new Vector2 (transform.position.x, transform.position.y);
Wait
//same as before but switch to dim1
if (Input.GetKeyDown ("x"))
if (dimension2 == true)
dimension2 = false;
dimension1 = true;
transform.position = new Vector2 (transform.position.x, transform.position.y);
Wait
}
}
Answer by Maerig · Jun 09, 2014 at 01:39 AM
In C# yield
must be followed by a return
, and WaitForSeconds
is a type, so the proper syntax is
yield return new WaitForSeconds(3);
Now it gives Invalid token 'return' in class, struct, or interface member declaration, $$anonymous$$ethod must have a return type, and Type expected
Woops, I hadn't seen the rest of your script, silly me. It looks like you're trying to use Update
as a coroutine, but that's not possible. Could you please tell us what are you trying to achieve ?
Also, what are those lines for ?
IEnumerator Wait();
yield WaitForSeconds(3);
Did you intend to declare a method ?
I was trying to make a wait for use in the rest of the code, that was supposed to be a callable function that when used would wait for 3 seconds.
Answer by Landern · Jun 09, 2014 at 01:59 PM
Your code/script is malformed.
yield can not exist outside of a method that returns IEnumerator, your code need to yield and return or at the very least start a coroutine method.
also calling directly in the update method is bad, start a coroutine instead.
just noticed you're also missing curly braces.
public class dimensionswitcher : MonoBehaviour
{
bool dimension1 = true;
bool dimension2 = false;
bool waiting = false;
IEnumerator WaitASec(float seconds)
{
yield return new WaitForSeconds(seconds);
waiting = false;
}
void Update()
{
if (!waiting)
{
//if player hits z they switch to dim2 and cant switch for 3 sec
if (Input.GetKeyDown("z"))
{
if (dimension1 == true)
{
dimension2 = true;
dimension1 = false;
transform.position = new Vector2(transform.position.x, transform.position.y);
waiting = true;
StartCoroutine(WaitASec(3));
}
}
else if (Input.GetKeyDown("x"))
{
if (dimension2 == true)
{
dimension2 = false;
dimension1 = true;
transform.position = new Vector2(transform.position.x, transform.position.y);
waiting = true;
StartCoroutine(WaitASec(3));
}
}
}
}
}
Ok, so it has no errors now, however it doesnt function how I want it to. The way I planned it was you should not be able to switch to dimension 1 within 3 seconds of switching to dimension 2 and vice versa, however this does not seem to be working.
Okay, so now if waiting is true, it skips the whole key down evaluation, sets back to false once the wait of 3 seconds is over, etc.
Your answer
Follow this Question
Related Questions
Ending my game 3 Answers
Monodevelop crashes after change in code 0 Answers
C# transform position 1 Answer
BCE0044: expecting }, found 'private'. PLEASE HELP!!!!! 2 Answers
How do I change script API reference language in Monodevelop? 1 Answer