Newb Programming Question - Object.Selected?

Hey guys - as the name suggests - trying to work through the giant hurtle of learning basic programming to get my game in the works. What I’m trying to do is make an RTS, and I’m simply writing code for the player to select a building (transform via the camera [player’s perspective]), it to look like it was selected, and then do various things from that.

So I’ll figure out the rest, however, I’m stuck at making the object selected. Basically, what I’m figuring, logic wise, is I want to write an if statement - if the transform (building) is selected, then draw the graphic of a green circle below the building (so you know it’s been selected). How would one do that? There’s no such thing as a transform.selected, eh?

In pseudocode I want this:

if (transform.Selected) {
greenCircle.enable(etc, etc, etc)

However, I’m guessing it’d be easier to just instantiate a green circle that I can just destroy later:

if (transform.Selected) {
Instantiate(greenCircle, GameObject.Find(“selectedGraphicspawnPoint”).transform.position, Quaternion.identity);

This is probably way messier than you gurus would probably do it, however the “transform.Selected” is obviously make believe. How would I go about this? How would I even allow a transform to be selected by the mouse?

You can use a raycast to find the transform that the mouse has clicked. In order for it to work, the transform (unit) will need a Collider component.

Reposting some raycast selection code from another answer:

function Update ()
{
   if ( Input.GetMouseButtonDown(0) )
   {
      var hit : RaycastHit;
      var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
      if (Physics.Raycast (ray, hit, 100.0))
      {
         Debug.Log(hit.collider.gameObject.name);
         // in here, you would set 'hit' to be 'selected'
      }
   }
}

Where it says, ‘// in here’, you would also set the green selection marker position (and possibly parent, so it moves with the unit that you have ‘selected’).

Let me know if you have any questions!