- Home /
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"){
if(playSound){
audio.PlayOneShot(Sound);
playSound = false;
collider.enabled = false;
var instance : GameObject = Instantiate(thePrefab, transform.position, transform.rotation);
};
}
}
any help is appreciated
Answer by HSMInteractive · Aug 15, 2013 at 01:33 AM
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);
Destroy(this);
}
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.
Your answer
Follow this Question
Related Questions
Room generator spawn 1 unique room 1 Answer
Spawn Script not working on Iphone. 1 Answer
Random Spawning 1 Answer
How to set up random spawnpoints? 1 Answer
How to make levels spawn randomly? 0 Answers