Help with C# Array Please

Hey guys,

I’m working on a project, and right now I am trying to get the “tab” key to cycle through my enemies, however I can’t get it to fill my list with my tagged targets. Can someone please take a look at my code, and tell me why the list won’t populate at game start? I think there is something going wrong in my AddAllEnemies() Method, but I can’t for the life of my figure out what it is. Thanks so much in advance guys…any help would be greatly appreciated. :sunglasses: (And just FYI…I have tired the script to my player). lol

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

public class Targeting : MonoBehaviour {
public List targets;
public Transform selectedTarget;

private Transform myTransform;

// Use this for initialization
void Start () {
targets = new List();
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();
}

}
}

It works fine on my end, when I look in the inspector I can see all my targets in the list.
I pasted your script and attached it to my camera and created 10 enemy objects.

Really??

What version of Unity are you using?
Have you upgraded to version 4, and is that where it worked?

I’m wondering if there is something changed in a package that I just don’t know about or if I’m missing something.

Thanks again

Did you make a new tag “Enemy” and assign it to the game objects you want to be enemies? If your not receiving errors in the console then you may have to do something in the inspector.

Yes, each of my enemies have the “enemy” tag on them, as I simply duplicated the first one. Can someone please confirm that this is working with unity 4? Im starting to wonder if I should downgrade. Thanks again all for you help. It’s nice to have a place like these forums to go. :slight_smile:

Can you just double check they have the “Enemy” tag on them? Works fine on my end as well. I’m using unity 4, even though that doesn’t matter.

When you initially create the tag, it doesn’t assign that game object the actual tag. You have to go assign it again.

Also just double check it is “Enemy” and not “enemy”

OMG!! Guys I’m so sorry…Thank you “DarkHazard”, …I feel like such a noob. The prb was the fact that my tags were “enemy”, but my code was “Enemy”. LOL, …what happens when staring at code to long I guess. Thanks again!!!