Yield return new WaitForSeconds(2); don't work

Hi all, i have a problem with my code, the code ensures that when the level is finished it will load a new scene, but i want that it 2 sec. wait with loading the scene, so i did that with: Yield return new WaitForSeconds(2);, and used the IEnumerator, but my whole code didn’t work anymore, does somebody know what the problem is? i’m btw not a pro in this things. I use C# for 4 months now. I hope somebody can help me, thanks!

public bool[] holeTrigger = new bool[6];

	protected string currentLevel;
	protected int worldIndex;
	protected int levelIndex;
	// Use this for initialization
	void Start () {
		//save the current level name
		currentLevel = Application.loadedLevelName;

	}
	
	// Update is called once per frame
	void Update () {
		//move the player based on left and right arrow keys
		transform.Translate(Input.GetAxis("Horizontal")*Time.deltaTime*10f, 0, 0); //get input
	}

	

	public IEnumerator TriggerHole(int index) {
		holeTrigger[index] = true; //Set the hole trigger to true
		
		//Check if all holes have been triggered
		if (AllTriggered) {     
			UnlockLevels ();   //unlock next level funxtion 
                    Yield return new WaitForSeconds(2);
			Application.LoadLevel ("fLevel1");
		}
	}
	
	//This method will iterate through each of the triggers in the array and return false if any of them are not true.
	public bool AllTriggered {
		get{
			bool b = true;
			foreach(bool bX in holeTrigger){
				if(!bX)
					b = false;
			}
			return b;
			;}
	}

	protected void  UnlockLevels (){
		//set the playerprefs value of next level to 1 to unlock
		for(int i = 0; i < LockLevel.worlds; i++){
			for(int j = 1; j < LockLevel.levels; j++){               
				if(currentLevel == "Level"+(i+1).ToString() +"." +j.ToString()){
					worldIndex  = (i+1);
					levelIndex  = (j+1);
					PlayerPrefs.SetInt("level"+worldIndex.ToString() +":" +levelIndex.ToString(),1);
				}
			}
		}

	}

The line you’ve inserted is not correct. You wrote “Yield” with the capital Y; moreover, where are you calling the TriggerHole() function?

By the way, in C#, you should do this:

  1. Create an IEnumerator, as you did. Making it simple (just because I want to correct your “Yield” mistake):

    IEnumerator Wait(){
    yield return new WaitForSeconds(2);
    Application.LoadLevel(“fLevel1”);
    }

  2. Then, call it this way:

    StartCoroutine(“Wait”);

Use a Lower case for the ‘y’ of yield.

yield return new WaitForSeconds(2);

Hi man, your code works, but i have the same problem witht the code, a timer counts down and when it reach 0 it will a failed scene, but what happens now is that when you finish the level it wait 2ms and than it loads a failed scene, but it must load a succesfull scene (“fLevel”).