I have watched through the whole video lots of times now and I can’t figure out what’s causing the problem.
It say that there is an error with my NextFreePosition.
Assets/Scripts/EnemySpawner.cs(84,15): error CS0161: `EnemySpawner.NextFreePosition()’: not all code paths return a value
I am using Microsoft Visual Studio btw. I don’t know if there it is different from Mono Develop. Just thought I would let you know if there is.
Here is my Script:
public GameObject enemyPrefab;
public float width = 10f;
public float height = 5f;
public float speed = 5f;
public float spawnDelay = 0.5f;
private bool movingRight = false;
private float xMax;
private float xMin;
void Start()
{
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, 1, distanceToCamera));
xMax = rightBoundary.x;
xMin = leftBoundary.x;
SpawnEnemies();
}
public void OnDrawGizmos()
{
Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
}
void SpawnUntilFull()
{
Transform freePosition = NextFreePosition();
if (freePosition)
{
GameObject enemy = Instantiate(enemyPrefab, freePosition.position, Quaternion.identity) as GameObject;
enemy.transform.parent = freePosition;
}
if (NextFreePosition())
{
Invoke("SpawnUntilFull", spawnDelay);
}
}
public void SpawnEnemies()
{
foreach (Transform child in transform)
{
GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
enemy.transform.parent = child;
}
}
void Update()
{
if (movingRight)
{
transform.position += Vector3.right * speed * Time.deltaTime;
}
else
{
transform.position += Vector3.left * speed * Time.deltaTime;
}
float rightEdgeOfFormation = transform.position.x + (0.5f * width);
float leftEdgeOfFormation = transform.position.x - (0.5f * width);
if (leftEdgeOfFormation < xMin){
movingRight = true;
} else if (rightEdgeOfFormation > xMax){
movingRight = false;
}
if (AllMembersDead())
{
Debug.Log("members dead");
}
}
Transform NextFreePosition(){
foreach (Transform childPositionGameObject in transform){
if (childPositionGameObject.childCount == 0){
return childPositionGameObject;
}
}
}
bool AllMembersDead()
{
foreach (Transform childPositionGameObject in transform)
{
if (childPositionGameObject.childCount > 0)
{
return false;
}
}
SpawnEnemies();
return true;
}

