How to make a selection system?

Hi, I’m trying to make a selection system. At the start of the game it will ask you to select(click) the character you want. If you selected it there will be a small triangle underneath the character to show that you selected it and will disappear if you selected a different character. The code is attached to all 4 of the characters. Heres the code I tried:

#pragma strict
var orangeSelect : GameObject;
var pinkSelect : GameObject;
var blueSelect : GameObject;
var greenSelect : GameObject;
static var orangeSelection : boolean;
static var pinkSelection : boolean;
static var blueSelection : boolean;
static var greenSelection : boolean;

function Start () {
	orangeSelection = true;
	orangeSelect.SetActive(true);
}

function Update () {
	if(Input.GetMouseButtonDown(0)) {
		var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
	    var hit : RaycastHit;
	    if (collider.Raycast(ray, hit, 100.0)) {
	    	if( hit.collider.gameObject.name == "Orange")
             {
                 if(pinkSelection == true||blueSelection == true||greenSelection == true) {
                 	orangeSelection = true;
                 	pinkSelection = false;
                 	blueSelection = false;
                 	greenSelection = false;
                 	BlahBlah ();
                 	if(orangeSelection == true) {
                 		orangeSelect.SetActive(true);
                 	}
                 	if(orangeSelection !== true) {
                 		orangeSelect.SetActive(true);
                 	}
                 }
             }
             if( hit.collider.gameObject.name == "Pink")
             {
                 if(orangeSelection == true||blueSelection == true||greenSelection == true) {
                 	pinkSelection = true;
                 	orangeSelection = false;
                 	blueSelection = false;
                 	greenSelection = false;
                 	BlahBlah ();
                 	if(pinkSelection == true) {
                 		pinkSelect.SetActive(true);
                 	}
                 	if(pinkSelection !== true) {
                 		pinkSelect.SetActive(true);
                 	}
                 }
             }
             if( hit.collider.gameObject.name == "Blue")
             {
                 if(pinkSelection == true||orangeSelection == true||greenSelection == true) {
                 	blueSelection = true;
                 	pinkSelection = false;
                 	orangeSelection = false;
                 	greenSelection = false;
                 	BlahBlah ();
                 	if(blueSelection == true) {
                 		blueSelect.SetActive(true);
                 	}
                 	if(blueSelection !== true) {
                 		blueSelect.SetActive(true);
                 	}
                 }
             }
             if( hit.collider.gameObject.name == "Green")
             {
                 if(pinkSelection == true||blueSelection == true||greenSelection == true) {
                 	greenSelection = true;
                 	pinkSelection = false;
                 	blueSelection = false;
                 	orangeSelection = false;
                 	BlahBlah ();
                 	if(greenSelection == true) {
                 		greenSelect.SetActive(true);
                 	}
                 	if(greenSelection !== true) {
                 		greenSelect.SetActive(true);
                 	}
                 }
             }
	    }
	}
}

function BlahBlah () {
	yield WaitForSeconds (0.1);
}

It kind of worked but theres still some problems:

  1. It only detects my click when I clicked right in the middle of the characters.
  2. The triangle wouldn’t disappear after I clicked a different character.

Does anyone have any ideas why this happen? I checked my script several times but I can’t anything wrong with it. Thanks

Hello markzareal,

Here is main logical flow of solution .

  1. Generate only one script which have all record of your current status. means script similar to your created.(I called this file PlayerSelectionManager)

  2. make one other script for every object you need to detect touch event. (I called this file touchDetector.)

  3. touchDetector file have only touch detect code like onMouseDown() and send massage to PlayerSelectionManager when onMouseDown called.

  4. perform action according to that event in PlayerSelectionManager.

I am not expert in javascript so i code in c#.

  1. PlayerSelectionManager

    public class PlayerSelectionManager : MonoBehaviour {

     GameObject orangeSelect ;
     GameObject pinkSelect ;
     GameObject blueSelect ;
     GameObject greenSelect ;
    

    // int SelectedId;
    bool orangeSelection;
    bool pinkSelection ;
    bool blueSelection ;
    bool greenSelection ;
    // Use this for initialization
    void Start () {

     	orangeSelection = true;
     	orangeSelect.SetActive(true);
     }
    
     void ChangeSelection(string objectName)
     {
     	BlahBlah ();
    
     	switch(objectName)
     	{
     	case "Orange" :
    
     		pinkSelection = false;
     		blueSelection = false;
     		greenSelection = false;
    
     		if(orangeSelection == true) {
     			orangeSelect.SetActive(true);
     		}else if(orangeSelection != true) {
     			orangeSelect.SetActive(true);
     		}
    
     		pinkSelect.SetActive(false);
     		blueSelect.SetActive(false);
     		greenSelect.SetActive(false);
    
     		break;
    
     		case "Pink" :
    
     		orangeSelection = false;
     		blueSelection = false;
     		greenSelection = false;
    
     		if(pinkSelection == true) {
     			pinkSelect.SetActive(true);
     		}
     		if(pinkSelection != true) {
     			pinkSelect.SetActive(true);
     		}
    
     		orangeSelect.SetActive(false);
     		blueSelect.SetActive(false);
     		greenSelect.SetActive(false);
    
     		break;
     		// And so on.
     	}
    
     }
    
    
     void BlahBlah()
     {
     }
    

    }

  2. touchDetector

    public class touchDetector : MonoBehaviour {

     void OnMouseDown() {
     	SendMessageUpwards ("ChangeSelection", name);
    
     }
    

    }

Note. Here i use SendMessageUpwards() so every orangeSelect,pinkSelect,… are child of gameObject which have PlayerSelectionManager script attach to it. (it is not must you can use SendMessage() it is unto you.)
(It is not tested)