Re-spawn Object Help!

*Hello,
Im doing a college project in which I have to send OSC messages to Pure Data, I need that object to re-spawn once it is destroyed by the collision with the ball (player)… Once the ball collides with the object, OSC is sending 1000s of messages to pure data instead of one. I want a single message to be sent when I hit the coin and then I want the coin to then, re-spawn.
This is the code!

#pragma strict

var coinEffect : Transform;

var oscRef : OSCTestSender2Jump;

var respawnTimer : float;

var collectedItem : float;

var delayTime : float;

var objectPrefab : Transform;

function OnTriggerEnter (info : Collider)

{
if (info.tag == “Player”)

{
    Debug.Log("Add coin counter here!");
    Instantiate(coinEffect, transform.position, transform.rotation);
    oscRef.coinCollide();
    Destroy(gameObject);

    if(collectedItem){
        respawnTimer += Time.deltaTime;
        if(respawnTimer > delayTime){
            var newObject = Instantiate(objectPrefab, transform.position,
            transform.rotation);
            respawnTimer = 4.0;
        }
    }
}

}

Thanks!

why dont you use a bool as a flag eg . private bool flag = true;
before collision just check if the flag is true once the collision is success turn the flag to be false ;

hope this will help

Please check if the “Add coin counter…” message is logged once or multiple times.

If once, then problem probably lies inside coinCollide method. If multiple times, then it means that new instance of coin is immediately created, collide with player, send message, is marked as destroyed, spawn another coin, and the process continues. This can happen, if collectedItem condition is true and deltaTime <= 4.0

On the other hand, if deltaTime is greater than 4.0, instantiate code will probably never be called.

Anyway, the code won’t work properly. I suggest that you don’t destroy the object at all and then reinstantiate it, but just hide it for a given amount of time, and then show it again.