[SOLVED] AllMembersDead parsing errors?

Hey all, I need some fresh eyes. As far as I can tell, my AllMembersDead function is IDENTICAL to the one Bryce is using in the video. I watch as his works normally while mine throws two errors preventing me from testing anything further.

bool AllMembersDead(){
	foreach(Transform childPositionGameObject in transform){
		if(childPositionGameObject.childCount > 0){
			return false;
		}
	}
	return true;
}

I get a CS1525 error for the top line, and a CS8025 on the “return true;” line. I’ve triple checked against those errors and I can’t find anything wrong.

Thanks for your help!

edit: I forgot to mention, I am using Unity 4.7.2f1 instead of Unity 4.6.x, could that be causing something?

Hi @Jason_Johann,

I believe CS1525 is an unexpected symbol error, from memory. As your method looks ok, chances are there is an issue else where in your script. For example, having the closing curly brace for the class before this method perhaps.

Could you paste the entire EnemyFormation.cs file please.


See also;

Sure thing, thanks for the quick help!

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;
		}
		
		bool AllMembersDead(){
			foreach(Transform childPositionGameObject in transform){
				if(childPositionGameObject.childCount > 0){
					return false;
				}
			}
			return true;
		}
		
		if(AllMembersDead(true)){
			SpawnEnemies();
			Debug.Log ("Empty Formation");
		}
	}
}

and yeah, according to the docs the errors are for “unexpected symbol” and “missing/too many braces” respectively. :confused:

1 Like

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();
        }

OOOOH I see now. That did it, thanks so much! I didn’t spot in the video that it was outside the Update, I thought it was supposed to be inside.

Sorry about the code, I’ve bookmarked your link for future reference. Thanks so much! :smiley:

edit: Yep, just caught that (regarding adding in “true”). Remnant from what I did on my own, which was otherwise good at least, haha.

1 Like

No worries, and you are more than welcome.

No biggy on the code formatting, a lot of people just paste it straight in, it works for the most part but always leaves the using statement and class definitions out of the formatting, simply add three ``` characters before and after what you paste in resolves this :slight_smile:

Method definitions need to be in inside the class but not within another method. You can obviously call your methods from within another method.

Hope this is all of use :slight_smile:

1 Like

Privacy & Terms