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
}
}
In C# yield must be followed by a return, and WaitForSeconds is a type, so the proper syntax is
yield return new WaitForSeconds(3);
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));
}
}
}
}
}