I have tried in various methods, to add 5 waves to my scene with 5 enemies per wave (varying in challenge as you progress) that once destroyed move to the next wave and failed miserably.
Could anyone suggest how I can do this without changing the scene - To keep the health and score?
Really stuck pulling my hair out.
Any help is greatly appreciated.
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class EnemySpawner : MonoBehaviour {
public GameObject enemyPrefab;
public float width = 10f;
public float height = 5f;
public float speed =5f;
public float spawnDelay = 0.5f;
private bool movingRight = true;
private float xmax;
private float xmin;
private LevelManager levelManager;
// Use this for initialization
void Start () {
//Calculate gamespace boundary
float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
Vector3 leftBoundary = Camera.main.ViewportToWorldPoint (new Vector3 (0, 0, distanceToCamera));
Vector3 rightBoundary = Camera.main.ViewportToWorldPoint (new Vector3 (1, 0, distanceToCamera));
xmax = rightBoundary.x;
xmin = leftBoundary.x;
SpawnUntilFull ();
}
//Spawn Emenies Function
void SpawnEnemies(){
//Get child of EnemyFormation - spawn Enemy underneath Position
foreach (Transform child in transform) {
//Enemy Spawner appear in EnemyFormation
GameObject enemy = Instantiate (enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
//SpawnUntilFull function
void SpawnUntilFull(){
Transform freePosition = NextFreePosition ();
//Check if free position exists
if (freePosition) {
GameObject enemy = Instantiate (enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
enemy.transform.parent = freePosition;
}
//Delay and only re spawn if position is empty
if (NextFreePosition ()) {
Invoke ("SpawnUntilFull", spawnDelay);
}
}
//Width and height of EnemyFormation bounding box
public void OnDrawGizmos(){
Gizmos.DrawWireCube(transform.position, new Vector3(width, height, 0));
}
// Update is called once per frame
void Update () {
//How to move the formation side to side and bouce off the edge of the screen
if (movingRight) {
transform.position += Vector3.right * speed * Time.deltaTime;
}
else {
transform.position += Vector3.left * speed * Time.deltaTime;
}
//Bounce the bounding box EnemyFormation off the edge og the screen
float rightEdgeOfFormation = transform.position.x + (0.5f * width);
float leftOfEdgeOfFormation = transform.position.x - (0.5f * width);
if (leftOfEdgeOfFormation < xmin) {
movingRight = true;
} else if (rightEdgeOfFormation > xmax) {
movingRight = false;
}
//Check for all the enemies present
if (AllMembersDead ()) {
//Debug.Log ("Empty Formation");
//Debug.Log("NEW LEVEL SHOULD LOAD");
//levelManager.LoadNextLevel();
//yield return new WaitForSeconds(1.0f);
//SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
SpawnUntilFull ();
}
}
//Find the next free position for re-spawning
Transform NextFreePosition(){
foreach (Transform childPositionGameObject in transform) {
if (childPositionGameObject.childCount == 0) {
return childPositionGameObject;
}
}
return null;
}
//AllMembersDead function - Check enemies are dead or not
bool AllMembersDead(){
foreach (Transform childPositionGameObject in transform) {
if (childPositionGameObject.childCount > 0) {
return false;
}
}
return true;
}
}