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();
}
}