Radio button like toggle behaviour on gameobjects

Hi.
Heres what I would like to achieve.
I have at least three gameobjects.
If I click on one object(out of three) it would have arbitrary boolean value wasClicked set true. If I click on any other gameobject then the boolean for the previously clicked object gets set to false and gets set true for the last object that was clicked. So all the three gameobjects would use the wasClicked boolean but it would be true for only one gameobject at a time.

So far my idea was to

  1. collect the gameobjects in to array
  2. Create a extension method for the wasClicked boolean
  3. Somehow do the radio button like behaviour???

Heres some cut outs from my code

Game object array

private GameObject[] players; // Create array for gameobjects


void Start ()
	{	
		players = GameObject.FindGameObjectsWithTag("Player");//Collect all the right gameobjects
	}

void OnMouseDown()
	{
			RaycastHit hit ;//Set up new raycasthit
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//Set up new ray shooting out of the camera from where the mouse is
			
			if (Physics.Raycast(ray, out hit))
	        {	
				int i = 0;
				players *= hit.transform.gameObject;//The Parent object that was hit by ray*

_ GameObject myObj = players*;//Convert array object to parentObject for nicer syntax*_

* if (myObj.wasClicked)//if == true*
* {*
* //do something with the gameobject*
* }*

_ else if (/Another object was clicked /)_
_
{
_
* //Set wasClicked false for the object that was clicked first and do something*
* }*

* }*

}
Extension method code
using UnityEngine;
using System.Collections;
public static class toggleObject {

public static void wasClicked(this GameObject go, bool clickState ){

* clickState=false;*
* }*

}
If anybody could at least tell me if I’m on the right way with using array’s that would be awesome.
And for the boolean… I think my gameobjects (myObj) will end up having child objects added to them when they are clicked… so i think i could use something like
bool hasChild = myObj.transform.hasChildren();//Pseudo code
But I would like to avoid that if possible.

You seem like the kind of person who likes to think about these things, so I won’t give you any working code. Instead, I will tell you where you’re going wrong with this approach.

The way you explain using one boolean to mark the state for multiple objects cannot work. A boolean is just true of false, you cannot infer the object it is pointing to. What you could do is use an int to mark which object it is pointing to in the array. So when changing the boolean you would also change the int. But on further thought the boolean is then irrelevant, as it’s always true. Following this trail of thought, why not keep a reference to the currently selected GameObject itself instead of an int. This way, the radio button effect will have been achieved, because only one GameObject can be pointed to at any given time.

The current way you’re using your array overrides some references to players, namely to the player #0. So you will not be able to reference that player again unless you regain the reference. It puzzles me why you modify the array at all, because you already have the reference to the clicked object from hit.transform.gameObject.

Finally, your extension method doesn’t actually do anything. clickState will be a local variable within the method, so changing it will not have any effect globally. If you feel the need to use this approach (I think it’s overcomplicating things) you need to do some research on how to pass variables by reference.

To check for children, you’ll need to use transform.childCount. I’d imagine there isn’t a better way.