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
- collect the gameobjects in to array
- Create a extension method for the wasClicked boolean
- 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.