RTS Style Selection system (How to control one unit and not the others)

Okay, so far I have it so that when I click somewhere, an object(MouseMarker) is created there, the unit(a simple cube) looks at the object and moves towards it. When the cube hits the MouseMarker the marker is destroyed, and so the cube stops. Now, I want it so that if I have 2 (or more) cubes, that set of script is only executed if the cube is selected. Being new to unity, I dont really understand how to make scipts only apply to one instance of an object, not the entire object as a whole. Any explanations or links to tutorials would be great, thanks (:

The answer is a static variable where you assign the current selected GameObject. Setup a manager (empty GameObject with a script named “manager”) which contains these kind of global variables:

static var unit : GameObject;

On the object set the variable (which is done by a raycast):

function OnMouseDown () {
    manager.unit = this.gameObject;
}

OnMouseDown/OnMouseUp is short for raycasting towards a specific object. You’ll have an advantage in setting up a global raycast by yourself and by checking the objects tag though. It depends a bit on how your world looks like.

Then you’ll be able to steer the specific object by referencing to its transform:

manager.unit.transform

Don’t have the steering for each object on their GameObject, that would result in plenty of if-statements or workarounds, think global and manage the objects from outside. Although if it’s easier for the implementation, you can attach a steering script for each selected object and then destroy it when it’s no longer needed.

var steerScript : SteerScript;
function OnMouseDown () {
    manager.unit = this.gameObject;
    manager.unit.AddComponent(SteerScript)
}

function Unselect () {
    if (manager.unit.GetComponent(SteerScript))
        Destroy(manager.unit.GetComponent(SteerScript);
}

If you have several objects to steer at once you’d only need to make an array where you assign everything in a for-loop.

you can find all the simple source and really good general knowledge here. But do the guy a favor and SUBSCRIBE him. Theres links to his sourc codes tutorials projects all that. Also you’ll need the A star pathfinding that he provides a link to in us second RTS tutorial… Like I said… Subscribe him… This dudes awesome to the community.