- Home /
how to destroy objects on contact
ok i have this script that is originally used to spawn enemies but i think i could use it to make items capable of being destroyed i tested the spawner by making it spawn cubes instead of enemies and the cubes have rigid bodies and when i knocked the cube over another one spawns there. im asking what will i need to change in the script to make it that when i shoot the cube over or just shoot it it will spawn a different (enemy) by enemy i mean a new block model instantly so that it looks like the person destroyed it here it is.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var amountEnemies = 20; // Total number of enemies to spawn.
var yieldTimeMin = 2; // Minimum amount of time before spawning enemies randomly.
var yieldTimeMax = 5; // Don't exceed this amount of time between spawning enemies randomly.
function Start(){
Spawn();
}
function Spawn(){
for (i=0; i
yield WaitForSeconds(Random.Range(yieldTimeMin, yieldTimeMax)); // How long to wait before another enemy is instantiated.
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)]; // Randomize the different enemies to instantiate.
var pos: Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
Instantiate(obj, pos.position, pos.rotation);
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613064333im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Destructible Environment 1 Answer
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
if object is destroyed 2 Answers
Destroying bullets after certain time 1 Answer
How could i optimize this code 2 Answers