rts loading bar not working

i’ve been trying to create an rts style recruitment system.where unit creation is loaded and you have to check for resources.here is how it goes:

1)i click on a gui button.

2)i started loading when the button is clicked.

3)i check for the gold (if enough do something,else no).

4)if both conditions are true. i insantiate a prefab.

EDIT:

updated the code as i was attempting to fix it myself. still not working thou.

problem:

the value of the loading bar gets incremented and resets properly. but for some reason the prefab doesn’t get created. i am a beginner in unity so if some one could tell me why it’s not working or how to go about fixing it i would be grateful.below are my scripts.

section where i create units.

	public int UnitCost() {
	if (prefab.name != TANK) {
		return BUILDER_COST;
	} else {
		return TANK_COST;
	}
}

public void CreateUnit() {
	if(SubtractGold(UnitCost())) {
		myLoadingBar.StartLoading();
		if(myLoadingBar.IsLoading!=true) {
			Debug.Log("IsLoading is "+myLoadingBar.IsLoading);
			InstantiateUnit();
		}
	}
}

public void InstantiateUnit () {
	if (myLoadingBar.IsLoading!=true) {
		var z = Instantiate(prefab,
                calculateSpawnPosition 
                (centerPos, spawnRadius), 
                Quaternion.identity) as GameObject; 
	}
}

loading bar script. i have a reference to it in my building script (one above).

public float Progress {
	get {
		return progress;
	}
	set {
		progress = value;
	}
}

public bool IsLoading {
	get {
		return isLoading;
	}
	set {
		isLoading = value;
	}
}

private float progress, fullBar, progValue;
private bool isLoading;

private void Start() {
	progress = 0f;
	fullBar = 50f;
	progValue = 0.5f;
	IsLoading = true;
}

public void StartLoading() {
	StopCoroutine ("Loader");
	StartCoroutine ("Loader");
}

IEnumerator Loader() {
	Progress = 0f;
	do {
		Progress+=progValue*Mathf.Clamp01(fullBar);
		Debug.Log(Progress);
		yield return new WaitForEndOfFrame();
	} while(Progress<fullBar);
	if (Progress>=fullBar) {
		Debug.Log("Hell");
		//Debug.Log("IsLoading is "+IsLoading);
		IsLoading = false;
	}
	yield return new WaitForEndOfFrame();
}

}

I think the problem is that you aren’t waiting for the bar to finish.

The process at the moment takes place in the same frame and looks like this:
Press button → CreateUnit → StartLoading → Check IsLoading

You only just called StartLoading, so unless it finishes instantly, IsLoading will be true still.

There are two approaches you can take.

1: Have the caller check every frame to see whether Loader finished. (simple, less efficient)

void Update() {
    if(myLoadingBar.Finished) {
        InstantiateUnit();
        myLoadingBar.Finished = false;
    }
}

public bool finished;
IEnumerator Loader(System.Action onComplete) {
    finished = false;
    // your code
    finished = true;
}

2: At the end of “Loader” you tell the caller you finished. (more efficient, more complex)

public void StartLoading(System.Action onComplete) {
    StopAllCoroutines();
    StartCoroutine (Loader(onComplete));
}
IEnumerator Loader(System.Action onComplete) {
    // your code
    onComplete(); // invoke the action
}

public void CreateUnit() {
    if(SubtractGold(UnitCost())) {
        myLoadingBar.StartLoading(InstantiateUnit);
    }
}

Note that neither solution will allow for multiple units being built at a time, but this should get you started