- Home /
Object Click Problem - Help
Hello,
I have some prefabs on my scene that are clickable. The click works, one problem though - I can multi-select different prefabs. How can I prevent this? So I can only click one object. I was thinking about having a GetMouseDown - on raycast, and if the raycast is positioned outside the prefab, then click = false ??
Watch YouTube Video: http://www.youtube.com/watch?v=3MUCktH47MM
ClickObject:
#pragma strict
var clicked : boolean = false; var hit : RaycastHit;
function Update() { if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)) { clicked = !clicked; if(tag == "Select") tag = "Unselect"; else tag = "Select";
}
}
Clickable:
function Update(){
var ExternalClickObject : ClickObject;
ExternalClickObject = gameObject.GetComponent("ClickObject");
var ExternalRadius : RadiusTexture;
ExternalRadius = gameObject.GetComponentInChildren(RadiusTexture);
if(ExternalClickObject.clicked == true){
ExternalRadius.Clicked = true;
}
else if(ExternalClickObject.clicked == false){
ExternalRadius.Clicked = false;
}
}
---- EDIT ----
I added this onto the end of ClickObject:
else if(Input.GetMouseButtonDown(0) && !collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity) && clicked == true) { clicked = !clicked; if(tag == "Select") tag = "Unselect"; else tag = "Select";
}
}
It works - so when ever I select another turret, the current turret gets deselected. BUT it also deselects the turret when I click anywhere -- including GUIs! Which I don't want?!?!
If you could provide a script that would be great!
$$anonymous$$ake a num_Selected = 0; And increment it everytime something is selected, then if num_Selected > 1; then you can just have it deselect the items (I think in your example, it'd be change the tags to "unselected"
How would in insert that? It sounds good but I have no idea how I would do that ...
Answer by Berenger · Dec 09, 2010 at 02:54 PM
As Justin Warner say, you need all your object to know about the selection status. A static var seems ok to me.
How would I go about inserting that? $$anonymous$$y coding sucks - would appreciate it. Thanks
$$anonymous$$hh, I'd say, start by creating a script called Click$$anonymous$$anager or something. Inside, create a variable like this "static var previousClickObject : ClickObject;". Then when there is a mouseDown && Raycast, I suggest you do if( Click$$anonymous$$anager.previousClickObject ) Click$$anonymous$$anager.previousClickObject.clicked = false; Click$$anonymous$$anager.previousClickObject = this; (and the tag thing). That should work. You might need to put the script Click$$anonymous$$anager on a GameObject to make it work though (I'm not sure about that).
Your answer
Follow this Question
Related Questions
How to change selected button in EventSystem or deselect it 5 Answers
Destroy a single prefab with a click ? 2 Answers
Selection and Deselection 1 Answer