Some "Unexpected symbol" errors I can't see to find

Looking for some keen eyes to help me locate this issue. I have three symbol errors which I can’t seem to located, and it’s been two days, a bit of hair pulling and a bit too much coffee.

Assets/Scripts/EnemySpawner.cs(86,32): error CS1525: Unexpected symbol `(', expecting `,', `;', or `='

Assets/Scripts/EnemySpawner.cs(93,18): error CS1519: Unexpected symbol `return' in class, struct, or 
interface member declaration

Assets/Scripts/EnemySpawner.cs(95,4): error CS1525: Unexpected symbol `}'

These are within the Update block, being the updates ‘}’ and two errors on the last block. It’s refering to the "return true; " and Transform " All EnemiesDead() { "

System.Collections;
using System.Collections.Generic;
using UnityEngine;

class EnemySpawner : MonoBehaviour {

public GameObject enemyPrefab;
public float width = 10f;
public float height = 5f;
private bool movingRight = false;
private bool movingLeft = false;
public float speed = 3f;
public float spawnDelay = 0.5f;
private float xmax;
private float xmin;
private float ymax;
private float ymin;


// Use this for initialization
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, 0, distanceToCamera));
    Vector3 upBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, distanceToCamera));
    Vector3 bottomBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, distanceToCamera));
    xmax = rightBoundary.x;
    xmin = leftBoundary.x;
    ymax = upBoundary.y;
    ymin = bottomBoundary.y;
    SpawnUntilFull();
}
 

void SpawnEnemies() {
    foreach (Transform child in transform) {
        GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
        enemy.transform.parent = freePosition;
    }
}

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 OnDrawGizmos() {
    Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
}

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

    Transform(NextFreePosition()); {
        foreach (Transform childPositionGameObject in transform) {
            if (childPositionGameObject.childCount == 0) {
                return childPositionGameObject;
            }
        }
        return null;
    }

    Transform AllEnemiesDead() {

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

Hi,

You have an erroneous semi-colon on line 77;

Transform(NextFreePosition()); {

Remove the semi-colon, the cascading errors will invariably disappear.

Hope this helps. :slight_smile:

Incidentally, if you try a block-style approach to the curly braces, rather than the trailing-style these things often stand out a little more easily, example;

Your code re-formatted

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

class EnemySpawner : MonoBehaviour 
{
	public GameObject enemyPrefab;
	public float width = 10f;
	public float height = 5f;
	private bool movingRight = false;
	private bool movingLeft = false;
	public float speed = 3f;
	public float spawnDelay = 0.5f;
	private float xmax;
	private float xmin;
	private float ymax;
	private float ymin;

	// Use this for initialization
	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, 0, distanceToCamera));
		Vector3 upBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, distanceToCamera));
		Vector3 bottomBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, distanceToCamera));
		xmax = rightBoundary.x;
		xmin = leftBoundary.x;
		ymax = upBoundary.y;
		ymin = bottomBoundary.y;
		SpawnUntilFull();
	}

	void SpawnEnemies() 
	{
		foreach (Transform child in transform) 
		{
			GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
			enemy.transform.parent = freePosition;
		}
	}

	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 OnDrawGizmos() 
	{
		Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
	}

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

		Transform(NextFreePosition());    // naughty semi-colon stowing away here
		{
			foreach (Transform childPositionGameObject in transform) 
			{
				if (childPositionGameObject.childCount == 0) 
				{
					return childPositionGameObject;
				}
			}
			return null;
		}

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

Hey Rob, thanks for the quick reply!

Oddly enough that actually gave me an extra error! It’s not only expecting a semi-colon where it was but this time redirecting me to the ’ { ’ following that “naughty semi-colon” as the new error. I believe I read that right the first time where you were suggesting I change

Transform(NextFreePosition()); { }

to

Transform(NextFreePosition()) { }

Hi Kyle,

Sorry, I should have checked a little more thoroughly myself, the line should read;

private Transform NextFreePosition()
{
    // ... rest of code between following { } characters
}

I’m pouring out the coffee Rob.

I’m getting these two errors after changing the parent to a private,

Assets/Scripts/EnemySpawner.cs(92,8): error CS1525: Unexpected symbol `private'
Assets/Scripts/EnemySpawner.cs(92,25): warning CS0642: Possible mistaken empty statement

Along with the previous three still present.

Assets/Scripts/EnemySpawner.cs(92,42): error CS1525: Unexpected symbol `('

Assets/Scripts/EnemySpawner.cs(101,18): error CS1519: Unexpected symbol `return' in class, struct, or 
interface member declaration

Assets/Scripts/EnemySpawner.cs(104,8): error CS1525: Unexpected symbol `Transform'

I’ve looked at some alternative ways to go around this with no success.

Reposting the whole code here:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

class EnemySpawner : MonoBehaviour
{
    public GameObject enemyPrefab;
    public float width = 10f;
    public float height = 5f;
    private bool movingRight = false;
    private bool movingLeft = false;
    public float speed = 3f;
    public float spawnDelay = 0.5f;
    private float xmax;
    private float xmin;
    private float ymax;
    private float ymin;

// Use this for initialization
    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, 0, distanceToCamera));
        Vector3 upBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, distanceToCamera));
        Vector3 bottomBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, 
distanceToCamera));
        xmax = rightBoundary.x;
        xmin = leftBoundary.x;
        ymax = upBoundary.y;
        ymin = bottomBoundary.y;
        SpawnUntilFull();
    }

     void SpawnEnemies()
    {
        foreach (Transform child in transform)
        {
            GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as 
GameObject;
            enemy.transform.parent = freePosition;
        }
    }

    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 OnDrawGizmos()
    {
        Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
    }

// 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;
        }

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

       private Transform NextFreePosition()
        {
            foreach (Transform childPositionGameObject in transform)
            {
                if (childPositionGameObject.childCount == 0)
               {
                    return childPositionGameObject;
                }
           }
            return null;
        }

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

From what i’m gathering, would I be on the right track to say ‘private’ wont work in this case?

Hi Kyle,

Wow, I was tired last night.

Ok, so the Update method is missing a closing brace, it is there, but you have the last two methods effectively inside it.

The NextFreePosition method is fine.

The AllEnenimesDead method is also fine.

I would, however, add the access modifier private to all of your methods which currently do not have an access modifier displayed. It is the default, so they are all private, but it would serve you better to know what these methods are doing whilst you are on your learning journey.

Corrected code below;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

class EnemySpawner : MonoBehaviour
{
    public GameObject enemyPrefab;
    public float width = 10f;
    public float height = 5f;
    private bool movingRight = false;
    private bool movingLeft = false;
    public float speed = 3f;
    public float spawnDelay = 0.5f;
    private float xmax;
    private float xmin;
    private float ymax;
    private float ymin;

	// Use this for initialization
    private 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, 0, distanceToCamera));
        Vector3 upBoundary = Camera.main.ViewportToWorldPoint(new Vector3(0, 1, distanceToCamera));
        Vector3 bottomBoundary = Camera.main.ViewportToWorldPoint(new Vector3(1, 1, distanceToCamera));
        xmax = rightBoundary.x;
        xmin = leftBoundary.x;
        ymax = upBoundary.y;
        ymin = bottomBoundary.y;
        SpawnUntilFull();
    }

    private void SpawnEnemies()
    {
        foreach (Transform child in transform)
        {
            GameObject enemy = Instantiate(enemyPrefab, child.transform.position, Quaternion.identity) as GameObject;
            enemy.transform.parent = freePosition;
        }
    }

    private 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 OnDrawGizmos()
    {
        Gizmos.DrawWireCube(transform.position, new Vector3(width, height));
    }

	// Update is called once per frame
    private 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())
        {
            SpawnUntilFull();
        }
	}

	private Transform NextFreePosition()
	{
		foreach (Transform childPositionGameObject in transform)
		{
			if (childPositionGameObject.childCount == 0)
		   {
				return childPositionGameObject;
			}
	   }
		return null;
	}

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

My apologies for missing this last night.

No need for apologies. Regardless of me combing through it I still didn’t find the missing bracket. But it’s necessary to learn to properly read/write the code right when faced with these errors and find the mistake. I’m just glad you have a better eye than I do. Thanks Rob!

To help, here’s an extract from the code you posted before my reply;

// Update is called once per frame
    void Update()
    { // move your mouse down the screen vertically from this opening brace
        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())
        {
            SpawnUntilFull();
        }

       // there should be a closing brace here for the Update method defined above

       // NextFreePosition is a new method and shouldn't be within another method
       private Transform NextFreePosition()
        {
            foreach (Transform childPositionGameObject in transform)
            {

            // ... rest of code follows from here

I’ve added a couple of comments to highlight the area.

1 Like

Privacy & Terms