- Home /
if(object exists in scene)
I have a spawn script and i want waves to start ONLY if there are no other enemies present in the scene, but i can't find a way to check if an enemy is instantiated. Here is my script:
using UnityEngine;
using System.Collections;
public class CrappyEnemySpawner : MonoBehaviour {
public GameObject Enemy;
public bool Enabled;
public GameObject SpawnPoof;
public float SpawnChanceIsOneIn;
public float CurrentWave;
public float EnemiesPerRound;
public float WaveStartDelay;
public float WaveDelay;
public bool Wave;
public GameObject SpawnedEnemy;
// Use this for initialization
void Start () {
SpawnedEnemy = GameObject.FindGameObjectWithTag("Enemy");
CurrentWave = 1;
Wave = true;
}
// Update is called once per frame
void Update () {
if (SpawnedEnemy.activeInHierarchy){
Debug.Log ("No Enemies");
}
if (Wave == false){
WaveStartDelay += 1 ;
}
if (WaveStartDelay > WaveDelay){
Wave = true;
}
if (SpawnChanceIsOneIn<50 && Wave == true){
SpawnChanceIsOneIn -= 3f;
} else {
SpawnChanceIsOneIn -= 0.1f;
}
if(Random.Range(1,100)>SpawnChanceIsOneIn && Enabled==true && Wave == true){
Instantiate(Enemy, transform.position, transform.rotation);
}
if(SpawnChanceIsOneIn<EnemiesPerRound && Wave == true){
Debug.Log ("Congrats! You've made it to wave" + CurrentWave);
CurrentWave += 1f;
WaveDelay *= 1f;
Wave = false;
EnemiesPerRound -= 1.5f;
SpawnChanceIsOneIn=100;
}
}
}
Thanks in advance!
Answer by Kiwasi · Oct 09, 2014 at 06:57 PM
I do this by making all enemies children of the same GameObject. Then you can check childcount.
Another viable alternative is to add each enemy to a list and check the length of the list
I've heard of "GameObject.ActiveInHierarchy" how would i use it and would it work for this?
Check out the docs. ActiveInHierachy just checks weather a GameObject is enabled or disabled (for various reasons it does not make sense to check a bool directly). Its probably not going to be much use to you.
Here is some pseudo code on using a List
public class Enemy {
public static List<GameObject> enemyList = new List<GameObject>();
void OnEnable (){
enemyList.Add(gameObject);
}
void OnDisable (){
enemyList.Remove(gameObject);
}
}
public class SomeOtherClass {
void Update (){
Debug.Log(Enemy.enemyList.Count);
}
}
Answer by ThePunisher · Oct 09, 2014 at 10:44 PM
It's actually a very simple thing to do. In the script that is instantiating them (CrappyEnemySpawner in this case lol) you will want to keep track of all the spawned enemies by adding to directly to a list after they get instantiated. Like this:
// List definition. This will track the enemies that have been spawned.
private List<GameObject> m_enemies = new List<GameObject>();
// Place where the enemy gets instantiated and added to the list.
GameObject enemy = Instantiate(Enemy, transform.position, transform.rotation);
m_enemies.Add(enemy);
// When an enemy dies and you destroy it you will want to remove it from the list.
m_enemies.Remove(enemyThatJustDied);
Destroy(enemyThatJustDied);
// Here is how you would check if there are no more enemies left.
If(m_enemies.Count <= 0)
{
//All enemies have died, execute code in here.
}
and my enemies get destroyed on their own, so will i still need the "Remove from list" part because i'm kind of finding it a bit questionable, as when it is destroyed it won't exist so the list won't find if (or will it just give an error
ooooh, you forgot to tell me to add using "System.Collections.Generic;"
but it doesn't seem to recognize the number of characters spawned
Sorry about the using statement. I use it so often I've put it in the defaults, and tend to forget to tell people about it.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Unexpected token: if 1 Answer
Script that stores gameobject hit by raycast keeps getting a NullReferenceException error. 2 Answers
How to tell if two blocks are right next to each other?(2D) 1 Answer
Proper way to Pool Object - Can't acces due to protection level 1 Answer