destroying instantiated groups of objects
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[i]);
}
}
}
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
Ins$$anonymous$$d of destroying all of your coins with a method, why not set a timer for them to be destroyed when you instantiate them? Destroy (newlyInstantiatedCoin, 5f); The Destroy method takes in a gameobject and also has the ability to take in a float for the amount of time in seconds you wish to be until it is destroyed. You will have to store an instance of your newly created coin and then use that instance to call the destroy method as I just showed you.
Answer by Bunny83 · Mar 01, 2016 at 02:08 PM
Like @savlon said above you might want to queue the coins for autodestruct at the time you instantiate them:
void SpawnCoin(Vector3 spawnPosition)
{
var instance = Instantiate(coinPrefab, spawnPosition, coinPrefab.transform.rotation);
Destroy(instance.gameObject, 5f);
}
Other ways would be to give each coin a script that destroys the object it's attached to automatically
public class AutoDestruct : MonoBehaviour
{
public float delay = 5f;
void Start()
{
Destroy(gameObject, 5f);
}
}
You can setup the time on the coin prefab itself be changing the "delay" variable in the inspector. Each coin will handle it's removing automatically.
Since you always have packs of coins you can also group them in one gameobject and put an AutoDestruct instance on the group instead:
// [ ... ]
Transform group = (new GameObject("coinGroup")).transform;
AutoDestruct script = group.gameObject.AddComponent<AutoDestruct>();
script.delay = 5f;
for (int i = 0; i < amountOfCoins; ++i)
{
Transform inst = SpawnCoin(pos);
inst.parent = group;
}
// [ ...]
Transform SpawnCoin(Vector3 spawnPosition)
{
return Instantiate(coinPrefab, spawnPosition, coinPrefab.transform.rotation). transform;
}
This will put all coins into an empty gameobject and we manually put the AutoDestruct script onto the group so there's less overhead since not every coin needs a script, just every coin group.
ps:
In general you might want to avoid using methods like "FindGameObjectsWithTag" if it can be avoided. Since you instantiate the objects manually you can simply keep track of them ins$$anonymous$$d of instantiate them, forget about them and then ask the engine to search for them again.
yeah thank you very much this is definitely the best way to do it, and i do know that "FindGameObjectsWithTag" is quite taxing on the program ill be using your first answer thank you very much
after trying to implement this i came across an issue with making each one an instance and destroying it so i used your second approach which is working perfectly thank you for the thoroughness of your answer
Your answer
Follow this Question
Related Questions
Issue iterating through gameObjects deleting and addding some 0 Answers
Problem with Destroy function with instantiated Prefabs 1 Answer
Add Clones of GameObject to List(array) 0 Answers
Instantiate() and Destroy() vs setActive() 1 Answer
When instantiating 3 clones are created rather then 1 2 Answers