Spawn Script Issue

Discussion in 'Unity Support' started by dreadt9, Jan 13, 2012.

  1. dreadt9

    dreadt9

    Member

    Joined:
    Jan 13, 2012
    Messages:
    140
    Hello,
    I'm having trouble with my spawn script which uses Activation and Deactivation to kill and spawn my monsters.
    Could someone please help me with what i'm doing wrong.

    Here is the code:

    var enemy : GameObject;
    var enemy2 : GameObject;
    var enemy3 : GameObject;
    var enemy4 : GameObject;
    var enemy5 : GameObject;
    var enemy6 : GameObject;
    var enemy7 : GameObject;
    var enemy8 : GameObject;
    var enemy9 : GameObject;
    var enemy10 : GameObject;
    var enemy11 : GameObject;
    var enemy12 : GameObject;
    var enemy13 : GameObject;
    var enemy14 : GameObject;
    var enemy15 : GameObject;


    function Start (){


    }




    function Update (){
    if (enemy.active == false){
    if (enemy3.active == false){
    enemy2.SetActiveRecursively(true);
    }
    }



    if (enemy2.active == false enemy.active == false){
    if (enemy4.active == false){
    enemy3.SetActiveRecursively(true);
    }
    }

    if (enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy5.active == false){
    enemy4.SetActiveRecursively(true);
    }
    }

    if (enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false) {
    if (enemy6.active == false){
    enemy5.SetActiveRecursively(true);
    }
    }

    if (enemy5.active == false enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy7.active == false){
    enemy6.SetActiveRecursively(true);
    }
    }

    if (enemy6.active == false enemy5.active == false enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy8.active == false){
    enemy7.SetActiveRecursively(true);
    }
    }

    if (enemy7.active == false enemy6.active == false enemy5.active == false enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy9.active == false){
    enemy8.SetActiveRecursively(true);
    }
    }

    if (enemy8.active == false enemy7.active == false enemy6.active == false enemy5.active == false enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy10.active == false){
    enemy9.SetActiveRecursively(true);
    }
    }

    if (enemy9.active == false enemy8.active == false enemy7.active == false enemy6.active == false enemy5.active == false enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy11.active == false){
    enemy10.SetActiveRecursively(true);
    }
    }

    if (enemy10.active == false enemy9.active == false enemy8.active == false enemy7.active == false enemy6.active == false enemy5.active == false enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy12.active == false){
    enemy11.SetActiveRecursively(true);
    }
    }

    if (enemy11.active == false enemy10.active == false enemy9.active == false enemy8.active == false enemy7.active == false enemy6.active == false enemy5.active == false enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy13.active == false){
    enemy12.SetActiveRecursively(true);
    }
    }

    if (enemy12.active == false enemy11.active == false enemy10.active == false enemy9.active == false enemy8.active == false enemy7.active == false enemy6.active == false enemy5.active == false enemy4.active == false enemy3.active == false enemy2.active == false enemy.active == false){
    if (enemy.active == false){
    }
    }
    }
  2. snortop

    snortop

    New Member

    Joined:
    Dec 16, 2011
    Messages:
    188
    What do you do to the object that will be Despawning/Deactivation/Destroyed?
    Do you use

    Destroy(thise);

    gameObject.SetActiveRecursively(false);

    gameObject.active = false;

    And that not a way i would have done it, but your choice.
  3. dreadt9

    dreadt9

    Member

    Joined:
    Jan 13, 2012
    Messages:
    140
    Sorry, I am new to scripting but...
    I Use seperate scripts for the Player Health, Player Attack, Enemy Health, and Enemy Attack.
    But to deactive I use Enemy Health, If the enemy's health is 0, it will deactivate the dead monster.
    Oh and for the Enemy Health, Attack, Player Health, Attack I use C-Sharp, for the Spawnsystem script (which uses activation and deactivation) I use Java script.

    Here is a bit of the enemy health script:

    void Update () {
    AddjustCurrentHealth(0);

    if(curHealth <= 0)
    {
    animation.Stop();
    animation.CrossFade("Death01");
    gameObject.SetActiveRecursively(false);
    }

    }
  4. dreadt9

    dreadt9

    Member

    Joined:
    Jan 13, 2012
    Messages:
    140
    Also, The reason I didn't use Instantiate for a spawn script because I always had the problem that the enemy (clone)'s would be able to attack me but I wouldnt be able to attack them.
  5. snortop

    snortop

    New Member

    Joined:
    Dec 16, 2011
    Messages:
    188
    What is it that "is" the problem?

    Maybe it in your check for enemy death.
    Either you have the 3 first enemy activated or it dosent seem to be able to activate 4-5.
    Which mean you dont get to activate anymore enemy.
    Cant see where 1-3 enemy be activated.

    What you want it to do?
  6. polytropoi

    polytropoi

    Member

    Joined:
    Aug 16, 2006
    Messages:
    609
    keep at it, the pain you're feeling will motivate you to learn arrays - try something like this:

    Code (csharp):
    1. var enemies : GameObject[];
    2.  
    3. function EnemyControl(activeEnemy : int) {
    4.  
    5.      for (var i = 0; i < enemies.length; i++) {
    6.  
    7.           if (i =! activeEnemy) {
    8.    
    9.                 enemies[i].SetActiveRecursively(false);
    10.  
    11.            } else {
    12.  
    13.                  enemies[i].SetActiveRecursively(true);
    14.  
    15.      }
    16. }
    and just tell your control function which enemy to turn on by passing it a value, i.e. EnemyControl(3), that is it's index value in the array. Note that the first element in an array is 0, not 1.
    Last edited: Jan 14, 2012
  7. dreadt9

    dreadt9

    Member

    Joined:
    Jan 13, 2012
    Messages:
    140
    The problem is that when the first enemy dies and the second enemy spawns, when the second enemy dies he continuesly activates and deactivates in the area it dies so the third enemy can't spawn.