Controlling One of Several Similar Playing Pieces

hello, I'm trying to create a little tactical squad shooter where you control your squad one man at a time.

Story so far: I've made a prefab for the basic squad member.

I've made a Controller script which is attached to the Main Camera. It finds all the team members (using tags) and when I right click, the camera moves to the next squad members position and activates the mouselook and FPSWalker components.. so this allows you to control each team member one at a time.

The Problem: I'm now wanting to introduce some more complicated behaviour, such as, set a limit on how long each piece can be controlled for. So I've added a script to each SquadMember GameObject which contains:

static var MoveTime : int;
var moveTime : int;

function Start () {
MoveTime = moveTime;
}

function Update () {
}

So then using the Unity interface, I've set one Squad member's moveTime to 3 seconds, and one to 6 seconds. The Controller script is then meant to read this information and use it to control how long each man can be moved for:

// get all playing pieces in a gameobject array
friendlyPieces = GameObject.FindGameObjectsWithTag("RedPlayer");

/*... some code I've missed out which updates activePiece when I click the mouse... */

currentPiece = friendlyPieces[activePiece]; 
// move camera
transform.position = currentPiece.transform.position;
transform.rotation = currentPiece.transform.rotation;
transform.parent = currentPiece.transform; 
// enable player control
currentPiece.GetComponent(MouseLook).enabled = true;
currentPiece.GetComponent(FPSWalker).enabled = true;
currentPieceMoveTime = currentPiece.GetComponent(PlayerStats).MoveTime

However, this doesn't seem to work. Although the camere correctly moves to a new tagged GameObject when I update activePiece, it always reports the same currentPieceMoveTime.

Any suggestions? Am I right in thinking I need to learn how to use classes??

Thanks

Did you count down your moveTime when the piece is active?

This isn't line-for-line code with your script, but you'd want to do something like this:


function Update()
{
     if(isActivePiece)
     {
          // Subtracts time in seconds. deltaTime is (float) time
          // in seconds since last Update() call.
          moveTime -= Time.deltaTime;
     }
}

It doesn't look like you have the "isActivePiece" bool, so you'll need to create that yourself.