simplified spawn script?

In the game i’m making, i want to have enemies and items spawn on the map. The enemies spawn is fine, but the item spawn is what i need help on.

i already have a adequate spawning script, but i’m not sure how to change it from a player collision script, to a one time spawner that spawns a random item from a list, and either stop, or remove itself.

this is the java script

var Sound : AudioClip;

var playSound : boolean = true;

var thePrefab : GameObject;

function OnCollisionEnter (myCollision : Collision) {
 if(myCollision.gameObject.tag == "Player"){



    playSound = false;
    collider.enabled = false;
	var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);


any help is appreciated

Well, if the position of the spawner doesn’t matter, then create an empty GameObject and name it “Spawner” or something relevant. Now I would create an array for all your items like so:

var spawnableItems : GameObject[];

Then add a variable that will store the random item.

private var spawnedObject : GameObject;

And in the Start function, add the following:

function Start()

     spawnedObject = spawnableObjects[Random.Range(0,spawnableObjects.Length)];

     var instance : GameObject = Instantiate(spawnedObject, transform.position,         transform.rotation);


This will pick a random item to spawn, spawn it, and delete itself. I hope that’s what you were looking for.

If you don’t want it to spawn at the start, create your own function called “SpawnRandomItem” or something relevant and add the start function code in there. Then just call that function when needed.