Item Respawn Scripting?

Hello, I’m trying to get weapons in my game to respawn after x ammount of seconds. This is the current script that works great other then… if the item is alredy spawned it’ll keep spawning the item after each 10 seconds. So If you wait 20 seconds you have two of the same items… How can I get / change the script so that it detects if the item is still there or if the item was picked up by a player it’ll start the timer to respawn then activate the timer when a player picks it back up.

Here is the current script:

var cube : GameObject;
var spawn_position;
var timer = 0.0;

function spawn_cube ()
{
spawn_position = Vector3(1,1,1);
var temp_spawn_cube = Instantiate (cube, spawn_position, Quaternion.identity);
}

function Start () {

}
function Update () {
timer += Time.deltaTime;
if(timer > 20)
{
spawn_cube();
timer = 0.0;
}
}

IE: Player picks it up, timer starts, item spawns and waits to be picked up before timer and spawner start again.

Set a bool, isSpawned. When the player picks up the object, set isSpawned to false. When the object is spawned, set isSpawned to true before resetting the timer to 0. Only respawn the object if both isSpawned == false AND timer > spawnTime.

Examples…?

var bool: isSpawned;

......
function spawn_cube(){
    isSpawned=true;
    spawn_position = Vector3(1,1,1);
    var temp_spawn_cube = Instantiate (cube, spawn_position, Quaternion.identity);
  
}

function ClaimItem(){
    isSpawned=false;
    timer = 0;
}

function Update(){
    timer += Time.deltaTime;
    if(!isSpawned && timer >20){
        spawn_cube();
    }
}

You’ll need some system of accessing this script and calling ClaimItem() whenever someone walks over and picks this up. There are a few ways to handle this - it depends on how you’ve set up the code to pick up the weapon (is the item on the ground destroyed?).