Coin pickup problems D:

I’m pretty new to this so any help would be much appreciated!
I’m trying to setup coins with pickup animations in my 2D platformer, my current code is:

public class CoinScript : MonoBehaviour
{
    public GameObject coinPickup;


    void OnCollisionEnter2D (Collision2D col)
    {
        if (col.gameObject.tag.Equals ("Player"))
        {
            Instantiate(coinPickup, transform.position, transform.rotation);
            Destroy(gameObject);
        }
    }

It works fine when picking up the first coin ie the coin disappears and an animation is played (But only if it’s coin1 o.o) however when I then move to pickup a second coin it tells me the game object has already been destroyed and I end up kicking the coin away (they all have Rigidbody’s, I like em bouncy!)

The code seems to be removing the effect associated with the Instantiate line from other coins using the same script after it’s been ran once, is there an easy way to fix this?

What does the instantiate do? You shouldn’t need that. If each coin has coinscript it should only play animation and check for animation.isPlaying = false then destroy

Sorry perhaps I wasn’t clear, Instantiate triggers a particle effect. The problem is all other coins lose that effect and so the script can’t complete when I run into a second coin.

are the coins prefabs or duplicates ? Make the prefab from the coin and use it.

2 Likes

Thanks vaka, that’s fixed it!