quick question when my player hits death he loses all his coins
if (collision.collider.tag.ToLower() == "death" && amountOfCoins >= 1)
{
ContactPoint contact = collision.contacts[0];
Vector3 pos = contact.point;
for (int i = 0; i < amountOfCoins; ++i)
{
SpawnCoin(pos);
}
globals.allCoinsCollected = 0;
allGlobals.allGlobalCoins = 0;
}
heres that SpawnCoin method
void SpawnCoin(Vector3 spawnPosition)
{
Instantiate(coinPrefab, spawnPosition, coinPrefab.transform.rotation);
}
these coins are collectable for a few seconds and then are destroyed like this
void DestroyAllObjects()
{
newCoins = GameObject.FindGameObjectsWithTag("newcoins");
if (newCoins != null)
{
for (var i = 0; i < newCoins.Length; i++)
{
Destroy(newCoins*);*
}
}
}
my problem is if you hit death, the player loses all his coins, these are dispersed around the level, and coins are collectable for 5 seconds, now say you hit death again within this 5 seconds a new group of coins are instantiated but they are all destroyed together by my DestroyAllObjects method, so whats the best way around this so that each group of coins instantiated are destroyed according to there own 5 seconds, i was thinking of either putting my destroy method inside a co routine inside my spawncoin method or about adding a new tag to my coinPrefab and adding 1 to the tag for each group of instantiated coins, im kinda a noob at this though so any food for thought on this would be appreciated. Many thanks for reading. Any and all suggestions welcome