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

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