- Home /
c# Unexpected symbol 'foreach'
Assets/script/targeting.cs(22,31): error CS1525: Unexpected symbol foreach' I am writing a script to target monster in my game. But i ran across this problem that i just cant seem to find a solution to! Here have a look at the code..
using UnityEngine; using System.Collections; using System.Collections.Generic;
public class targeting : MonoBehaviour { public List targets; private Transform selectedTarget;
// Use this for initialization
void Start () {
targets = new List<Transform>();
selectedTarget = null;
AddAllEnemies();
}
public void AddAllEnemies()
{
GameObject[] go = GameObject.FindGameObjectsWithTag("enemy")
foreach(GameObject enemy in go)
AddTarget(enemy.transform);
}
public void AddTarget(Transform enemy)
{
targets.Add(enemy);
}
private void TargetEnemy()
{
selectedTarget = targets[0];
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
} `
hence the part where it give me unexpected symbol is "foreach(GameObject enemy in go)" i had already set up what go mean.. just the foreach part that unity is having trouble with.
Answer by Dakwamine · Dec 11, 2011 at 10:39 PM
Missing a ";" character at the end of the previous line :
GameObject[] go = GameObject.FindGameObjectsWithTag("enemy"); // <= Finish your line with ";" :)
Your answer
![](https://koobas.hobune.stream/wayback/20220613062003im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How can i have 3 weapon to switch between?? 1 Answer
First rpg how to shoot arrows ? 7 Answers
Can I make a choice-driven game? 3 Answers