Hi,
Again, please do refer to the guide I provided you with the link for, it makes the code easier to read for those trying to help you. I have amended your post so that it is formatted accordingly.
Regarding your issue, you have inserted the method AllMembersDead()
within the Update()
method.
Move this code;
bool AllMembersDead(){
foreach(Transform childPositionGameObject in transform){
if(childPositionGameObject.childCount > 0){
return false;
}
}
return true;
}
and place it before the final closing curly brace of the class, it will then be in its own method.
Example;
using UnityEngine;
using System.Collections;
public class EnemySpawner : MonoBehaviour
{
public GameObject enemyPrefab;
public float speed, width, height;
private bool movingRight = false;
private float minX, maxX;
// Use this for initialization
void Start()
{
float distanceToCamera = transform.position.z - Camera.main.transform.position.z;
Vector3 leftEdge = Camera.main.ViewportToWorldPoint(new Vector3(0, 0, distanceToCamera));
Vector3 rightEdge = Camera.main.ViewportToWorldPoint(new Vector3(1, 0, distanceToCamera));
minX = leftEdge.x;
maxX = rightEdge.x;
SpawnEnemies();
}
void SpawnEnemies()
{
foreach (Transform child in transform)
{
GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position, new Vector3(width, height, 0));
}
// Update is called once per frame
void Update()
{
if (movingRight)
{
transform.position += (Vector3.right * speed * Time.deltaTime);
}
else
{
transform.position += (Vector3.left * speed * Time.deltaTime);
}
// Constrains formation within the playspace
float rightEdgeOfFormation = transform.position.x + (0.5f * width);
float leftEdgeOfFormation = transform.position.x - (0.5f * width);
if (leftEdgeOfFormation <= minX)
{
movingRight = true;
}
else if (rightEdgeOfFormation >= maxX)
{
movingRight = false;
}
if (AllMembersDead(true))
{
SpawnEnemies();
Debug.Log("Empty Formation");
}
}
bool AllMembersDead()
{
foreach (Transform childPositionGameObject in transform)
{
if (childPositionGameObject.childCount > 0)
{
return false;
}
}
return true;
}
}
You will probably get an error for this bit of code next;
if (AllMembersDead(true))
{
SpawnEnemies();
Debug.Log("Empty Formation");
}
this is because the AllMembersDead()
method doesn’t take a parameter, what you want to do is see if it returns true;
if (AllMembersDead())
{
SpawnUntilFull();
}
would work, or, longer;
if (AllMembersDead() == true)
{
SpawnUntilFull();
}