Transform not working as it did before...

I have a transform object for my characters in my army, but only one responds to my commands. The script looks similar to…

var team : Transform;

function Update(){
   team = GameObject.FindWithTag("T1").transform;
}

function OnGUI(){
   if(GUI.Box(new Rect(0, 0, 100, 50), "Command"){
      team.SendMessage("Command1");
   }
}

I have been looking at the old code that worked, but I can’t find the problem?

thx in advance!

team is a single Transform in your code above. But you are saying an ARMY? Maybe you should be using a Transform instead, or System.Collections.Generic.List… If you want to find multiple objects with tag, use an array or list.

Here’s an example of how to use an list, I’ve found it to work out quite well.

#pragma strict
import System.Collections.Generic;

var team : List.<GameObject>;

function Awake()
{
    team = new List.<GameObject>();
    
    for(var t : GameObject in GameObject.FindGameObjectsWithTag("T1"))
    	team.Add(t);
    	
    print(team.Count);
}

I’m assuming that what you are trying to do is to create a number of Characters, and give each Character a reference to a transform which it can then send messages to.

GameObject. FindWithTag will only ever return 1 game object that is tagged with the provided tag name (see Unity - Scripting API: GameObject.FindWithTag)

I would suggest restructuring how you do things. For example, it might make sense to place each Character script on the GameObject that has the transform you are trying to access. In which case, you can get a reference to the transform via GetComponent or even just the “transform” convenience accessor.

What’s more, if you are just trying to a message, you could call that directly on the gameObject accessor.