Right now in my game, I'm effectively destroying objects that are a clone of another. However, when the game spawns enemies and spawns multiple clones, if I destroy an enemy that is a similar clone before I destroy another, it deactivates the code for that object and doesn't allow me to destroy it. In simple terms, when I destroy an object that is the same kind of another object, it won't allow me to destroy the earlier instance of that object. What I'm noticing it's doing is when I hit another object with my bullets, it's finding an earlier version of the clone and destroying that instead of destroying the character that the bullet is hitting. I understand why the code is doing what it's doing, but I'm just curious as it how I can stop it from happening and have my bullets destroy the object that it's actually hitting?
function OnTriggerEnter(collision:Collider) {
newSeed = Random.Range(1.0, 5.0);
if(gameObject.name == "Enemy_Pink(Clone)") {
if(collision.gameObject.tag == "Bullet") {
pinkLives--;
if(pinkLives == 0) {
Destroy(GameObject.Find("Enemy_Pink(Clone)"));
if(newSeed == 1) {
spawnAirTank(0);
}
playerScore += 10;
}
}
}
if(gameObject.name == "Enemy_Blue(Clone)") {
if(collision.gameObject.tag == "Bullet") {
blueLives--;
if(blueLives == 0) {
Destroy(GameObject.Find("Enemy_Blue(Clone)"));
if(newSeed == 2) {
spawnAirTank(1);
}
playerScore += 30;
}
}
}
if(gameObject.name == "Enemy_Red(Clone)") {
if(collision.gameObject.tag == "Bullet") {
redLives--;
if(redLives == 0) {
Destroy(GameObject.Find("Enemy_Red(Clone)"));
if(newSeed == 3) {
spawnAirTank(2);
}
playerScore += 50;
}
}
}
if(collision.gameObject.tag == "Bullet") {
Destroy(collision.gameObject);
}
totalScore += playerScore;
Debug.LogError("Player Score: " + totalScore);
}
↧