How to use WaitForSeconds inside IF / Else If statement?

Hey gang,

I am having a problem creating a pause in between two positions of a single gameObject.

I put a Cube in my scene. I then created a duplicate of the Cube and left it in the same position, but I hid the Box Collider and Mesh Renderer.

Next, I created a second duplicate of the Cube and did the same. This time I moved the Cube a small distance away.

I created a C# script that successfully moves/rotates/scales the Cube from position1 to position2.

Now I want to insert a 3 second pause after the Cube reaches each position.

I have attempted using WaitForSeconds MANY different ways, but can’t seem to get it to work when I am using IF / ELSE IF or ChangeTarget.

Any tips?

here’s my code:

using UnityEngine;
using System.Collections;

public class MoveCube : MonoBehaviour {

	public Transform moveCube;
	public Transform position1;
	public Transform position2;
	public Vector3 newPosition;
	public Quaternion newRotation;
	public Vector3 newScale;
	public string currentState;
	public float lerpMove;
	public float resetTime;
	
	void Start () {
		ChangeTarget ();
	}
	
	void FixedUpdate () {
		moveCube.position = Vector3.Lerp (moveCube.position, newPosition, lerpMove * Time.deltaTime);
		moveCube.rotation = Quaternion.Lerp (moveCube.rotation, newRotation, lerpMove * Time.deltaTime);
		moveCube.localScale = Vector3.Lerp (moveCube.localScale, newScale, lerpMove * Time.deltaTime);
	}
	
	void ChangeTarget (){
		
		//if we are at position1, then move to position2
		if (currentState == "Moving to position 1") {
			currentState = "Moving to position 2";
			newPosition = position2.position;
			newRotation = position2.rotation;
			newScale = position2.localScale;
		}

		//ADD 3 SECOND PAUSE HERE

		//if we are at position2, then move to position1
		else if (currentState == "Moving to position 2") {
			currentState = "Moving to position 1";
			newPosition = position1.position;
			newRotation = position1.rotation;
			newScale = position1.localScale;
		}

		//ADD 3 SECOND PAUSE HERE

		//the game first starts from the default state of the gameObject and moves to position2
		else if (currentState == "") {
			currentState = "Moving to position 2";
			newPosition = position2.position;
			newRotation = position2.rotation;
			newScale = position2.localScale;
		}
		Invoke ("ChangeTarget", resetTime);
	}
}

WaitForSeconds is a special class. It’s used as a coroutine’s yield object. You can’t use it in any function: the function must be a coroutine. Coroutine functions have a return type of IEnumerator.

For example:

//moves this object to the origin, waits for 2 seconds, then moves it to (10,10,10)
IEnumerator MyCoroutine(float someParameter)
{
    transform.position = Vector3.zero;
    yield return new WaitForSeconds(2f);
    transform.position = new Vector3(10f,10f,10f);

}

Note that coroutines require that you have at least one yield statement in them. If you’re wanting to work around that while testing, just add a yield break to the very end of your method. It’s a special yield object that causes the coroutine to exit early.

To start a coroutine, you need to call StartCoroutine(). For example, StartCoroutine(MyCoroutine(5f)); will start the above coroutine.