- Home /
Unexpected symbol `}', expecting `;' and else
I actually have a lot of problem over here. I'm quite sure I'm doing this right:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Targetting : MonoBehaviour {
public List<Transform> targets;
public Transform selectedTarget;
private Transform myTransform;
// Use this for initialization
void Start () {
targets = new List<Transform> ();
selectedTarget = null;
myTransform = transform;
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 SortTargetsByDistance()
{
targets.Sort(delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position))
});
}
private void TargetEnemy()
{
if(selectedTarget == null)
{
SortTargetsByDistance();
selectedTarget = targets[0];
}
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Tab))
{
TargetEnemy ();
}
}
}
But these errors keep showing up:
Assets/Scripts/Targetting.cs(38,17): error CS1525: Unexpected symbol }', expecting
;'
Assets/Scripts/Targetting.cs(42,15): error CS1525: Unexpected symbol private', expecting
;'
Assets/Scripts/Targetting.cs(52,14): error CS0116: A namespace can only contain types and namespace declarations
Assets/Scripts/Targetting.cs(58,1): error CS8025: Parsing error
I've been looking for the failiures for more than a day, and I couldn't find it. Does anyone have any idea what's wrong with it?
Note: I'm no pro, so sorry.
Answer by Visal · Mar 27, 2014 at 09:57 AM
I think maybe you're missing the big boss ;
after the return statement.
targets.Sort(delegate(Transform t1, Transform t2) {
return Vector3.Distance(t1.position, myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position));
});
Please mark this answer as a correct answer if it solved your problem.
Glad I could help :)
Answer by haim96 · Mar 25, 2014 at 02:46 PM
if you cut and paste this code make sure you don't have line breaker (enters) where you don't need them.
to make sure, put all long line in single line.
like this:
targets.Sort(delegate(Transform t1, Transform t2) {return Vector3.Distance(t1.position,myTransform.position).CompareTo(Vector3.Distance(t2.position, myTransform.position))});
It doesn't seem to be working. I tried to cut and paste the code and I tried to re-code it myself as well but it didn't work. Thank you btw
Answer by screenname_taken · Mar 27, 2014 at 10:06 AM
Line 38, you have a ) after the }. That confused it i think, remove the ). (I think so anyway. haven't done anything with CS yet.)
Your answer
Follow this Question
Related Questions
Ray Cast Click on Enemy - Change Current Target 1 Answer
Raycast target sight? 0 Answers
revise enemy code to player code 0 Answers