Welcome!

Welcome to our community forums, full of great people, ideas and excitement. Please register if you would like to take part.

This is extra text with a test link..

Register Now

Announcement

Collapse
No announcement yet.

when child reaches zero enemies spawn in non stop

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • when child reaches zero enemies spawn in non stop

    I'm trying to instantiate a new group of enemies once the player manages to shoot every enemy in the parent group however when I do that the counter for the number of children stay at zero as a non stop wave of enemies spawns in. I know this is because the original parent that the script is referencing is still at zero since all I am doing is spawning in a new group. My question is, Is there a way to either repopulate original group of children to the parent, or is there a way to replace the old parent with the newly instantiated group?

    code for reference

    if(enemyHolder.childCount == 0)
    {
    Instantiate(enemyGroup, enemySpawn.position, enemySpawn.rotation);



    }

  • #2
    There are probably several ways to solve this problem. If you always have only one active group of enemies, you can use a simpler mechanism that just tracks how many badGuys there are.

    So you have a ScoreKeeper script that has a variable called badGuys.
    When you instantiate an enemy group, you set
    Code:
    badGuys = 20; //or however many bad guys you have
    Then, on that ScoreKeeper you have a public method that can reduce bad guys

    Code:
    public void TrackBadGuys()
    {
      badGuys--;
      if(badGuys <= 0)
      {
        //Spawn another group
      }
    }
    When each bad guy dies, he calls out to the ScoreKeeper

    Code:
    if(health <= 0)
    {
      GameObject.Find("ScriptHolder").GetComponent<ScoreKeeper>().TrackBadGuys();
    }

    This way you're not worried about children, you're just tracking number of bad guys.

    Comment


    • #3
      when I destroy an enemy my bad guy count doesn't go down at first I thought it was because the script was on a group holding a group of enemies, but I applied it to the enemies themselves and the counter is still not going down.

      Here's the codes that are interacting with each other to try to achieve this

      Comment


      • #4
        Dead men tell no tales. If you've already destroyed the object, it can't do any work via scripts, because it doesn't exist anymore.

        The way you're set up, you could put the line:

        Code:
        GameObject.Find("ScriptHolder").GetComponent<PlayerScore>().TrackBadGuys();
        into your BulletController script (right after you destroy other, but before you destroy the gameObject. BTW, any code that happens after Destory(gameObject) will never fire because the object the script is attached to is gone.

        Comment


        • #5
          ahhh alrighty ill try this out!

          Comment

          Working...
          X