# Instantiate problem

Hello there, Have a bit of a problem here, need help on solving this problem.

I created this instantiate script to spawn collectable item(coins) in my game. All work as I wanted. the coins spawn by it “coinCount” and have random range between each coin. If a special trigger hit the coins it will destroy the coins and spawn a new set to replace the destroy coin. Below is my script:

``````using UnityEngine;
using System.Collections;

public class RandomCoin : MonoBehaviour
{
private int iCoin;
private Vector2 position;
private Quaternion rotation;

public Transform follow;

#region SPAWN RANDOM COINS
public GameObject[] coin;
public float coinCount;
public float coinRangeY;
public float coinRangeX;
public float coinStartY;
#endregion

private float random;

// Use this for initialization
void Start ()
{
rotation = Quaternion.identity;

for (iCoin = 0; iCoin < coinCount; iCoin++)
{
position = new Vector2 (Random.Range (-coinRangeX, coinRangeX), coinStartY + iCoin * coinRangeY);

random = Random.Range (1, coinCount);

if (random <= (0.5f * coinCount)) // 50%
{
Instantiate (coin [0], position, rotation);
}
else if ((random <= 0.8f * coinCount)  (random >= 0.5f * coinCount)) // 30%
{
Instantiate (coin [1], position, rotation);
}
else // 20%
{
Instantiate (coin [2], position, rotation);
}
}
}

// Update is called once per frame
void OnTriggerEnter (Collider hit)
{
rotation = Quaternion.identity;

if (hit.gameObject.layer == 11)
{
position = new Vector2 (Random.Range (-coinRangeX, coinRangeX), coinStartY + iCoin * coinRangeY);

random = Random.Range (1, coinCount);

if (random <= (0.5f * coinCount))
{
Instantiate (coin [0], position, rotation);
}
else if ((random <= 0.8f * coinCount)  (random >= 0.5f * coinCount))
{
Instantiate (coin [1], position, rotation);
}
else
{
Instantiate (coin [2], position, rotation);
}

iCoin++;

Destroy (hit.gameObject);
}
}
}
``````

Now here’s where the problem come in. I forgot that my charater can pick up the coins. So if my character pick up the coins the coins will be destroy but it will not spawn back the coin so when the special trigger hit the other coin that are not hit by the player it will still spawn the coin but below the value of “coinCount”. So how do I solved this problem.

BUMP

maybe make it check whats hitting it, making it only destroy if its the trigger. maybe have the trigger have a tag or the player have a tag which you can tell to ignore.

problem solve.