navmeshagent used in an rts game

hi… I am currently creating an rts game, i have decided to use navmeshagent to my characters in order to find the shortest path…i got a code from the forum which works efficiently but the problem is that the character that it moves is already specified…can you please help me convert the code in which it moves the selected character to the desired location…

the code is this…

public class Example : MonoBehaviour {
private NavMeshAgent agent;
void Start()
{
agent = GetComponent();
}
void Update() {
RaycastHit hit;
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
agent.SetDestination(hit.point);

    }
}

}

can you please help me???

Assuming this class is attached to the camera or some other global object, you could change the “agent” object as you select other objects and then when you select the other objects, so long as they have a NavAgent component, they will navigate using the script you currently have. So in short, every time you switch selected object’s change the agent variable from outside this script.