Curiosity over coroutine

here’s my code for testing coroutine

using UnityEngine;
using System.Collections;
using System.IO;

public class Text : MonoBehaviour {
	public GUIText TextOutput;
	//public TextAsset Tex;
	Cs Sc;
	private bool Ini=true;
	private int acc,Test=1;
	public GameObject Goat; 

	// Use this for initialization
	void Awake(){
		PlayerPrefs.GetInt("Gold");
		//PlayerPrefs.SetInt("Gold",0);
		Sc = Goat.GetComponent<Cs> ();
	}
	void Start () {
		StartCoroutine (MyCoroutine ());
		Debug.Log("What is happening "+ Test);
		TextOutput.text = "Gold " + PlayerPrefs.GetInt("Gold");
		//TextOutput.text="read";
	}
	**//You don't have to read the lines after this. They're not important-------------**
	IEnumerator MyCoroutine(){
	           while(Ini){
			if(Input.GetKeyDown(KeyCode.C)){
				Ini=false;
				acc=Sc.score;
				PlayerPrefs.SetInt ("Gold",acc+PlayerPrefs.GetInt("Gold"));
			
			  }
			yield return null;
		}
		Debug.Log("Finished"+ acc);
		yield return new WaitForSeconds(1f);
	}

	// Update is called once per frame

}

At first, I thnik my program would execute a coroutine first then it’ll print "What is happening "+ Test and TextOutput.text = "Gold " + PlayerPrefs.GetInt(“Gold”); respectively but the result is, they print the lines above even before the coroutine finished. Anyone knows the answer plss help. Thanks in advance

Your start method is launching the coroutine but it is not waiting for it.

You can do this in two ways:

void Start () {
   StartCoroutine (MyCoroutine ());
}
IEnumerator MyCoroutine(){
   while(Ini){
     if(Input.GetKeyDown(KeyCode.C)){
      Ini=false;
      acc=Sc.score;
      PlayerPrefs.SetInt ("Gold",acc+PlayerPrefs.GetInt("Gold"));

       }
     yield return null;
   }
   Debug.Log("Finished"+ acc);
   yield return new WaitForSeconds(1f);
   Debug.Log("What is happening "+ Test);
   TextOutput.text = "Gold " + PlayerPrefs.GetInt("Gold");
}

This one is how you should do it.

The one below is how you can do it:

IEnumerator Start () {
   yield return StartCoroutine (MyCoroutine ());
   Debug.Log("What is happening "+ Test);
   TextOutput.text = "Gold " + PlayerPrefs.GetInt("Gold");
}
IEnumerator MyCoroutine(){
  while(Ini){
     if(Input.GetKeyDown(KeyCode.C)){
      Ini=false;
      acc=Sc.score;
      PlayerPrefs.SetInt ("Gold",acc+PlayerPrefs.GetInt("Gold"));

       }
     yield return null;
   }
   Debug.Log("Finished"+ acc);
   yield return new WaitForSeconds(1f);
}