Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

What is the code for after you lost a round, all the remaining monsters will destroy?

Discussion in 'Scripting' started by MrBigDick92, Jul 3, 2020.

  1. MrBigDick92

    MrBigDick92

    Joined:
    Jul 27, 2019
    Posts:
    8
    Imagine the game Plant vs Zombies, after you lost a round/level, all the remaining attackers will destroy.

    Here's my code

    foreach (GameObject attacker in attackerPrefabs)
    {
    Destroy(attacker);
    }
    i also used SetActive(false) and did not work either


    I call this public method whenever my health turns to zero but it did not work and says i should not use Destroy and Destroy immediate.
     
  2. DroidifyDevs

    DroidifyDevs

    Joined:
    Jun 24, 2015
    Posts:
    1,724
    Make sure the attackerPrefabs are in the scene and actually references in that list/array. I recommend that to be a public list (List<GameObject> attackerPrefabs) and you can see what it contains in the inspector. The code should be fine - so I think you don't have the correct gameobjects in attackerPrefabs.
     
  3. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    Note that you don't want to destroy the actual prefabs (i.e. the assets), you want to destroy the instances of those prefabs in your level. Those are both of type "GameObject" but they're importantly different.