Search Unity

Destroying enemie prefab clones when running from battle

Discussion in 'Scripting' started by Josenifftodd, Dec 19, 2014.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I've made it so that the enemy spawns when I enter battle, I have a function that allows me to run from battle which works but the problem I'm having is the enemies which are prefab clones don't destroy themselves once I've left the battle so when I enter battle again they are still the same ones as before.. Anyone got any ideas?

    Code (JavaScript):
    1. // Enemy Prefabs
    2. var Enemy1 : GameObject;
    3. var Enemy2 : GameObject;
    4. var Enemy3 : GameObject;
    5.  
    6. var spawn1 : Transform;
    7. var spawn2 : Transform;
    8. var spawn3 : Transform;
    9.  
    10.  
    11. function Start () {
    12. spawnEnemies ();
    13. }
    14. function spawnEnemies () {
    15.         for(var i=0;i<3;i++) {
    16.                 var random1 : int = Random.Range(0,3);
    17.                 switch (random1) {
    18.                         case (0):
    19.                         var spawnEnemy1 = Instantiate(Enemy1);
    20.                         switch (i) {
    21.                                 case (0):
    22.                                 spawnEnemy1.transform.position = new Vector3(spawn1.transform.position.x, spawn1.transform.position.y, spawn1.transform.position.z);
    23.                                 spawnEnemy1.transform.tag = "enemy1";
    24.                                 break;
    25.                                 case (1):
    26.                                 spawnEnemy1.transform.position = new Vector3(spawn2.transform.position.x, spawn2.transform.position.y, spawn2.transform.position.z);
    27.                                 spawnEnemy1.transform.tag = "enemy2";
    28.                                 break;
    29.                                 case (2):
    30.                                 spawnEnemy1.transform.position = new Vector3(spawn3.transform.position.x, spawn3.transform.position.y, spawn3.transform.position.z);
    31.                                 spawnEnemy1.transform.tag = "enemy3";
    32.                                 break;
    33.                                 }
    34.                         break;          
    35.                         case (1):
    36.                         var spawnEnemy2 = Instantiate(Enemy2);
    37.                                 switch (i) {
    38.                                 case (0):
    39.                                 spawnEnemy2.transform.position = new Vector3(spawn1.transform.position.x, spawn1.transform.position.y, spawn1.transform.position.z);
    40.                                 spawnEnemy2.transform.tag = "enemy1";
    41.                                 break;
    42.                                 case (1):
    43.                                 spawnEnemy2.transform.position = new Vector3(spawn2.transform.position.x, spawn2.transform.position.y, spawn2.transform.position.z);
    44.                                 spawnEnemy2.transform.tag = "enemy2";
    45.                                 break;
    46.                                 case (2):
    47.                                 spawnEnemy2.transform.position = new Vector3(spawn3.transform.position.x, spawn3.transform.position.y, spawn3.transform.position.z);
    48.                                 spawnEnemy2.transform.tag = "enemy3";
    49.                                 break;
    50.                                 }
    51.                         break;
    52.                         case (2):
    53.                         var spawnEnemy3 = Instantiate(Enemy3);
    54.                                 switch (i) {
    55.                                 case (0):
    56.                                 spawnEnemy3.transform.position = new Vector3(spawn1.transform.position.x, spawn1.transform.position.y, spawn1.transform.position.z);
    57.                                 spawnEnemy3.transform.tag = "enemy1";
    58.                                 break;
    59.                                 case (1):
    60.                                 spawnEnemy3.transform.position = new Vector3(spawn2.transform.position.x, spawn2.transform.position.y, spawn2.transform.position.z);
    61.                                 spawnEnemy3.transform.tag = "enemy2";
    62.                                 break;
    63.                                 case (2):
    64.                                 spawnEnemy3.transform.position = new Vector3(spawn3.transform.position.x, spawn3.transform.position.y, spawn3.transform.position.z);
    65.                                 spawnEnemy3.transform.tag = "enemy3";
    66.                                 break;
    67.                                 }
    68.                         break;
    69. }
    70. }
    71. }
     
  2. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Alright, I see a number of of problems here.

    Getting a Random index of 0-3 results in 4 possible values, but you only consider 3 in your switch case. It should be Random.Range(0,2)

    If there is a reason you're giving your enemy game objects different tags, they should be more descriptive. Otherwise it should just be "Enemy". Tags offer a builtin way to find objects by type. Having two different tags called "enemy1" and "enemy2" serves no purpose.

    There is a lot of redundant code here. Your prefabs and transforms should really be in a builtin array, so that you wouldn't have to duplicate code while writing your switch case.
    This entire thing could be refactored as follows:

    Code (JavaScript):
    1. var enemies : GameObject[];
    2. var spawns : Transform[];
    3.  
    4. function Start(){
    5.     spawnEnemies();
    6. }
    7.  
    8. function spawnEnemies() {
    9.    
    10.     for (var i = 0; i < spawns.Length; i++) {
    11.    
    12.         var prefab_index : int = Random.Range(0, enemies.Length - 1);
    13.        
    14.         var enemy = Instantiate(enemies[prefab_index], spawns[i].position, Quaternion.identity);
    15.         enemy.tag = "enemy";
    16.  
    17.     }
    18.  
    19. }
    Finally, you've only supplied with part of your code. You've shown us how you create enemies, but not what happens when the battle ends. You don't call the Destroy() function anywhere here. Whatever function is called when the battle ends, you'd need to iterate through each of your enemy gameObjects and destroy them accordingly:

    Code (JavaScript):
    1. function endBattle() {
    2.    
    3.     var active_enemies = GameObject.FindGameObjectsWithTag("enemy");
    4.    
    5.     for(var enemy in active_enemies) {
    6.         Destroy(enemy);
    7.     }
    8.    
    9. }
    It might benefit you to take some coding tutorials! We all had to start somewhere, and I think you'll find you'll have a lot of confusing and frustrating problems if you don't have a good grasp on the basics. Making games is fantastic practice, but it would help if you had a good foundation to practice on.
     
  3. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Followed tutorials etc just didnt know you could do it other ways to how I've learnt lol.
    Cheers though ill read through what you've put and see what new things I learn. :)
     
  4. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Destroying the prefabs makes it so they don't respawn again when you next go into battle :/ lol
     
  5. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Y
    You can't destroy prefabs at runtime, it isn't possible, so you should be using prefabs from the project browser and not instancing gameobjects placed in the scene. The code I gave you destroys the instances of the prefabs, which would be the gameObjects in play.
     
  6. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Yup saved it as a prefab and put it into my list, it spawns before I even go into battle and then once I click Run to go back to the map it just doesn't come back when I go back to the battle plane thats under my map, would it be better to make a scene for the battle? :/
     
  7. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Once again, I'm going to suggest some fundamentals! I'd go back and do some more tutorials and really wrap your head around unity's workings. There are a lot of correct ways to create in unity, and without a strong understanding of how your project is set up, the answers I'd give you wouldn't be of much benefit.

    Tell you what though, upload your scene and additional code, I'll take a look and give you some pointers.
     
  8. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    will do ill upload it to dropbox and message you a link :) if the problem can be solved, i can post back on here with the answer. So people know if they come across this. :)