- Home /
Im getting this error CS0246
It says this error CS1031: Type expected and i dont know what to do or change so it is okay. Here is my script thanks for the help.
public class Targetting : MonoBehaviour {
public List targets;
private Transform myTransform;
public Transform selectedTarget;
// Use this for initialization
void Start () {
targets = new List();
selectedTarget = null;
AddAllEnemies();
myTransform = transform;
}
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];
}
else{
int index = targets.IndexOf(selectedTarget);
if(index < targets.Count - 1){
index++;
}
else{
index = 0;
}
DeselectTarget();
selectedTarget = targets(index);
}
SelectTarget();
}
private void SelectTarget(){
selectedTarget.renderer.material.color = Color.red;
PlayerAttack pa = (PlayerAttack)GetComponent("PlayerAttack");
pa.target = selectedTarget.gameObject;
}
private void DeselectTarget(){
selectedTarget.renderer.material.color = Color.blue;
selectedTarget = null;
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown(KeyCode.Tab))
{
TargetEnemy();
}
}
}
You need to indicate by commenting (not by list a line #) which line had the error. You can double click on the error in the Console to take you to that line in $$anonymous$$ono.
So, where is your type List declared? Are you missing a using?
Answer by theundergod · Feb 23, 2013 at 10:10 PM
List uses a Type so you need to declare what type of List it is using:
public List<ObjectType> targets;
then in your start function
void Start()
{
targets = new List<ObjectType>();
}
Answer by Eduardoxyz · Feb 24, 2013 at 02:08 AM
I dont know if im missing a using but it says it couldnt find it so it still has a mistake. my using are Unityengine System.collections and system.collections.generic.
even though i have these it still appears as if it cant find it
Your answer
Follow this Question
Related Questions
error CS0246: The type or namespace name `List' could not be found. 1 Answer
A node in a childnode? 1 Answer
UNity ads (in windows build) gives error (error CS0246: The type or namespace name `ShowResult' ) 3 Answers
[Google Cardboard SDK] Error Importing Package CS0246 0 Answers
itween imports into compile errors 1 Answer