- Home /
adiing a wait betweem if statements
hello everyone. Sorry to be asking yet another question, but I have a piece of code, where you can move back and forth along the z axis, yet I want there to be a wait between switching from one position to another. here is my code: using UnityEngine; using System.Collections;
public class dimensionswitcher : MonoBehaviour {
float dimension1 = 0;
float dimension2 = 1;
void Update () {
if (Input.GetKeyDown ("z"))
transform.position = new Vector3 (transform.position.x, transform.position.y, dimension1);
if (Input.GetKeyDown ("x"))
transform.position = new Vector3 (transform.position.x, transform.position.y, dimension2);
}
}
Answer by JayOhhh · May 29, 2014 at 07:20 PM
You need to use a Coroutine
Do ();
print ("This is printed immediately");
function Do () {
print("Do now");
yield WaitForSeconds (2);
print("Do 2 seconds later");
}
You will need to adjust your logic a little bit to make it work though, but basically...
public class dimensionswitcher : MonoBehaviour {
float dimension1 = 0;
float dimension2 = 1;
void Update () {
if (Input.GetKeyDown ("z")){
// Call coroutine
transform.position = new Vector3 (transform.position.x, transform.position.y, dimension1);
} else
if (Input.GetKeyDown ("x")){
// Call Coroutine
transform.position = new Vector3 (transform.position.x, transform.position.y, dimension2);
}
}
I have not fixed your logic for you to make it work, but you will need to move things around and use the coroutine to have a wait.
Also if you want a little bit of a hint you can take a look at my question I asked about coroutines:
http://answers.unity3d.com/questions/690943/shooting-bullets-onmousedrag-time-delay-issues.html
Hint is, it involves using a boolean.
Hah, I think you commented on the wrong thread :) But I can tell you your problem. If statements need to use comparison operators and not assignment operators (== vs =). (You are also missing the slashes on your comment.)
//call waitswitch
StartCoroutine (waitswitch());
if (dimension1) {
if (dimension2) {
Because they are booleans you don't need to have any comparison values in the if statement. If's are always asking for true false results. Because you have a boolean you can just go IF (boolean) or IF (!boolean).
Alternatively I believe you can go:
call waitswitch
StartCoroutine (waitswitch());
if (dimension1 == true) {
if (dimension2 == true) {
Also you are missing semi-colons everywhere! (line 6, 8, 23, 24, 26 and 2).
The other problem I see here is you still need to adjust your logic to work correctly. When you call your coroutine you are always setting both dimension variables to true. I don't think this is what you want.
Ok, I'm almost there now. Thanks to you all for bearing with me! I'm closer now, here's my new code: enter code hereusing UnityEngine; using System.Collections;
public class dimensionswitcher : $$anonymous$$onoBehaviour {
bool dimension1 = 0;
bool dimension2 = 1;
void Update () {
call waitswitch;
StartCoroutine (waitswitch());
if (dimension1){
if (dimension2) {
if (Input.Get$$anonymous$$eyDown ("z"))
transform.position = new Vector3 (transform.position.x, transform.position.y, dimension1);
if (Input.Get$$anonymous$$eyDown ("x"))
transform.position = new Vector3 (transform.position.x, transform.position.y, dimension2);
}
}
}
}
IEnumerator waitswitch(){
(dimension1 == false);
(dimension2 == false);
yield return new WaitForSeconds (3.0f);
(dimension1 == true);
(dimension2 == true);
}
but I get this error: Assets/dimensionswitcher.cs(21,30): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement
What exactly is the problem here? It compiles in $$anonymous$$ono develop, yet unity won't run it.
I suspect it's a problem with your assignment in the numerator.
== is for comparison
= is for assignment
(dimension == false) is the problem. You want to assign false to dimension. What you have is asking if dimension == false without an if statement.
I suggest reading up on C# and program$$anonymous$$g in general!
Your answer
Follow this Question
Related Questions
Making a bubble level (not a game but work tool) 1 Answer
Problem with player movements 1 Answer
Jump and move (CharacterController.velocity) C# 0 Answers
Unity and Custom Defines 1 Answer
Character Rotation 2 Answers