- Home /
Trouble with targeting enemies from a List
So I am following a tutorial on creating a simple combat system that is in C# and I am trying to convert it into unity script. I have the scene set up with a simple object that will be the player and three enemies. Each enemy is tagged "Enemy". I am trying to return their transforms into a List (later I'll change transform into something else, but I figured it was a good way to get it started). Here is the script Im using on the player object:
import System.Collections.Generic;
public var targets : List.;
public var go : GameObject[];
function Start() {
targets = new List.();
AddAllEnemies();
}
function AddAllEnemies(){
go = GameObject.FindGameObjectsWithTag("Enemy");
for(var enemy : GameObject in go){
AddTarget(enemy.Transform);
}
}
function AddTarget(enemy : Transform){
targets.Add(enemy);
}
So when I go into play mode and inspect my variables, the three enemies are put into the go variable correctly, but when I check the targets variable it is returned with three objects that read None (Transform). Im not sure where Im running into the problem. Any help would be appreciated.
Thanks
edit: the List. both have < Transform > after them. Not sure why it isn't showing up in the code
Answer by AlucardJay · Jan 22, 2013 at 05:06 AM
when you pass enemy to the function AddTarget, you have a capitol T in Transform, this should be lowercase t for transform. This should have popped up as an error in the console : BCE0019: 'Transform' is not a member of 'UnityEngine.GameObject'. Did you mean 'transform'?
Also are you using import System.Collections.Generic; for your list ?
Apart from that, your script is fine =]
#pragma strict
import System.Collections.Generic;
public var targets : List.<Transform>;
public var go : GameObject[];
function Start()
{
targets = new List.<Transform>();
AddAllEnemies();
}
function AddAllEnemies()
{
go = GameObject.FindGameObjectsWithTag("Enemy");
for(var enemy : GameObject in go)
{
AddTarget(enemy.transform);
}
}
function AddTarget(enemy : Transform)
{
targets.Add(enemy);
}
That did the trick. What exactly is the difference between Transform and transform? I know theres a difference between gameObject and GameObject too, but I'm not quite I grasp it.
I'm really not the person to answer that technical question, being a self-taught hobbyist! But my understanding is capitol Transform and GameObject refer to inbuilt classes (scripts within Unity that make everything work) and could also be called Components, and lowercase transform and gameObject are variables of a Component. That really doesn't hold because there is a special relationship between a gameObject and its transform, but for simplicity they are calling components or variables of that component.
Look at the API : http://docs.unity3d.com/Documentation/ScriptReference/GameObject.html
transform is a Variable of GameObject.
and with : http://docs.unity3d.com/Documentation/ScriptReference/Transform.html
gameObject is a Inherited Variable of Transform.
This is getting more confusing, so unless one of the pros can help out, it is easy just to think of capitol as a class which you are typecasting to , and lowercase is a variable of the component. Confused?! There is a special relationsip between A GameObject and a Transform, if you create an empty gameObject then it is both a GameObject, with a Transform component. They are best buddies, but that is going off-point to your question.
In your case, AddTarget(enemy.transform); is stating the transform variable of the GameObject enemy
Hello. Did this answer work, or did you find the suggestion useful? Please post a comment or mark an answer, for future reference by other people searching this 'site.
Oh crap sorry, I had the thumbs up but I didn't check it. Sorry! I think I need to go back to all my other posts here and check the thumbs up on em...
Thankyou, no worries. I had many answers without comments or feedback so last night I went through posting this everywhere. Am glad you got this working, thanks again, Happy Coding =]
Your answer
Follow this Question
Related Questions
How to find the opposite variables list? 1 Answer
instantiate problem help? 1 Answer
A node in a childnode? 1 Answer