- Home /
Destroy Istantiated objects after some time (different for each instance)
Greetings!! I'd like a suggestion on how to approach a problem i'm currently facing:
I'll outline the problem keeping everything else not relevant out of the window. Lets say i have two scripts: Player.cs Parts.cs
Player is unique, parts.cs is used by a Prefab and is used to spawn.. parts :)
Player have two int variables (CurrentSize and MaxSize) Player will spawn parts till MaxSize >= CurrentSize and the part spawned will have to be destroyed after "currentsize" seconds. After each spawn currentsize is increased and when a part is destroyed currentsize is decreased.
At this moment i am instantiating the prefab and in body.cs in the start function i have a reference to the player which will give me the currentsize and then launch a coroutine that will decrease the currentsize after x second and destroy the object.
What i believe is a better way to do it would be when from player.cs i instantiate with something like this:
Instantiate(BodyPrefab, transform.position, Quaternion.identity);
Store somehow a reference of the Objects i'm creating and destroying it from the player script (and also decreasing currentsize). To make it clear the parts i'm creating have all different time to live so i need to find the specific one i instantiate at that moment (another one created later have different time to live).
Hope i was clear enough! Any help is really appreciated:) Thanks Fabio
Answer by Albert · Sep 29, 2010 at 04:39 PM
If the parts' parent is the player, you could use the BroadcastMessage or SendMessageUpwards function to communicate between them.
You could add a function in your player.cs for adding/subtracting currentSize like:
public void changeSize(int amount)
{
currentSize -= amount;
}
then in your part.cs, you can make something like this:
public void killSelf(float timeKill) { StartCoroutine(killNow(timeKill)); }
public IEnumerator killNow(float timeKill)
{
yield return new WaitForSeconds(timeKill);
SendMessageUpwards("changeSize", 20, SendMessageOptions.DontRequireReceiver);
Destroy(gameObject);
}
since you said that every instance will kill itself depents on the currentSize amount, so maybe something like this will work, add this where you instantiate the parts.
GameObject newPart = Instantiate(BodyPrefab, transform.position, Quaternion.identity);
newPart.SendMessage("killSelf", currentSize);
Thank you for your input! Unfortunatly they are not parent/child classes. Would it be possible to store in an arraylist somehow the reference of the object being created and do all the destroying part from the player.cs script?
Just for test i made it a child, it works except the most important part. The public void killnow method doesnt get invoked and in the log i see "trying to invoke method couldnt be called"
hello, i edited the script above, the part.cs area. i dont know what's wrong with the invoke, so i changed it to StartCoroutine.
Greetings Clave! I'm giving you a thumb up for your help =) With the coroutine it does work. The sendmessageupwards doesn't but i worked around it by setting a reference to the player(which is unique) with the gameobjectfind and then accessing the currentsizevariable.
It now works, i still feel that it would be better to save a reference of the instance of the parts i create in the player (in an Arraylist) and destroy them from the player script after the condition is met (i.e. after a delay for instance.) but i don't know how to do it .. yet =)
yes, i think it's also possible using ArrayList, with that, you can manipulate everything in one script. goodluck :)