Hello there!
I’m instantiating coins randomly but I want to wait until the coins are added to the to the coin’s count. I don’t know why my code isn’t working:
//Instantiate a random number of coins and wait for add it to count
void AddCoins(){
StartCoroutine(Wait(1.0f));
for (int i = 0; i < ReturnRandomCoin(coinsEarned); i++){
GameObject NewCoin = (GameObject)Instantiate(myCoin, transform.position, Quaternion.identity);
}
}
//Calculate a random number
int ReturnRandomCoin(int RandomCoin){
int ret;
ret = Random.Range(3,7);
return ret;
}
//Wait for Adding the coin
IEnumerator Wait(float seconds){
yield return new WaitForSeconds(seconds);
playerScript.coins ++;
}
Anyway if I start the coroutine in Start() function it works, but if I do from my own void AddCoins() it never add the coins. What I am doing wrong?