I have cubes that are spawning in 1 second intervals and slowing moving across the screen. When you click on the cube prefab twice it should delete only that clone, but it is deleting all of the clones because they are all the same prefab. I have the spawning script set to an empty gameobject with this code:
#pragma strict
var enemy : GameObject;
var spawnWaitTime = 2;
function Start() {
InvokeRepeating ("Spawn", 1, spawnWaitTime);
}
function Update() {
}
function Spawn() {
var position : Vector3 = Vector3(10, Random.Range(-4.2, 4.2), 0);
Instantiate(enemy, position, transform.rotation);
Debug.Log(position.y);
}
As you can see, it is instantiating the “enemy” prefab. However, my other script which is attached to the enemy prefab has Destroy(gameObject); called when it is clicked twice. This destroys the enemy prefab rather than just the clone clicked on twice. Is there a way to destroy only the clone clicked on?
script attached to the enemy prefab:
#pragma strict
var enemyHealth = 10;
var clickDamage = 5;
function Start () {
}
function Update() {
if(Input.GetMouseButtonDown(0)) {
enemyHealth -= clickDamage;
}
if (enemyHealth <= 0) {
Death();
}
transform.Translate(Vector3.left * Time.deltaTime);
}
function Death() {
//gameObject.renderer.enabled = false;
Destroy(gameObject);
}