Question about coins

So I am working on a flappy bird game, most of it is done, score, highscore, pipes, going through etc.

But what I want to do is to have coins spawning randomly, I’ve done that by using parallex script that I am using on the pipes and clouds to move to left, and then I use its own score for the coins, everything works, the score adds and the coins spawn randomly BUT, I want them to disappear once collected. If I use GameObject(destroy); on it then it does not spawn anymore and I get shitload of errors with:

“the object of type transform has been destroyed but you are still trying to access it your script should either check if it is null or you should not destroy the object**”

I do get the coins, the coin does disappear but it doesn’t spawn anymore and I get tons of errors as I said before.

So is there a way to fix this? This is how the code looks normally:

**void OnTriggerEnter2D(Collider2D col)
{
        if (col.gameObject.tag == "coins")
        {
            CoinScript.coinValue += 10;
            Destroy(coin);
        }

Any help would be appreciated. As I said if I remove “destroy(coin);” the coins are spawning perfectly random, and I do get the points but I want the coin to DISAPPEAR once collected, this is impossible with destroy.

Hello, i imagine coin is a GameObject you are using as prefab, change the

Destroy(coin);

to

Destroy(col.gameObject);

mark it as correct answer if this correct your issue for helping future readers.

Nevermind. I fixed it by checking for null on my gameoverconfirmed script. Thanks for your help :slight_smile: IF anyone has this issue lemme know I’ll show you how to fix it.