- Home /
mouse select
ok so what i am trying to do is make a script, so that when i click an object a load of arrows come up next to the object, and then these will move the object. this is fine all works fine.
my real problem is when i click of the object, i want it to deselect the object.
i cant use exit as i also have a gui at the bottom which comes up at the bottem to rotate the object around.
so what i am looking for is a script that basically say if i touch this object, this and this shows up....i got that bit....
then if i click anything else apart from these objects or gui hide these objects.
i have made a script that allows me to hide them if i click the object again, but this is no good as i have a lot of these objects around.
i did try
var boo : boolean;
function Update(){
if(Input.GetMouseButtonDown(0)){
boo = false; }
if(Input.GetMouseButtonDown(0) == //my game object){
boo = true; } }
this is just an example not my actual script but you get the jist, i just dont know how to say ..
if mouse is not clicked on this object then boo is false. but if the mouse is on the object then boo is true
and then boo does the rest for me.
simple really im just a noob
ok so i am now using a yield for seconds 0.01 then enable the script where it says if i click down then disable and this works now, i can click the object over and over then when i click of it it disables it, but is there another solid way as i bet this way is very unstable and will eventually cause more problems than worth
Answer by reptilebeats · Jan 03, 2012 at 02:23 AM
so what i did was created a script that basically says if i click down the boo on another component equals false, and this component is deactivated. so something like this
function Start(){
GetComponent(Deselect).enabled = false;
}
function Update(){
if(Input.GetMouseButtonDown(0)){
GetComponent(show).boo = false;
GetComponent(Deselect).enabled = false;
}
}
then i have a script on my object which basically turns the boo true again and if the mouse leaves the object the component is enabled again on deselect. and if i go over my object then deselect is disabled again. if that makes sense. then i put that script on my GUI and whatever else i want. here is my other part of my script for that
var boo : boolean = false;
function OnMouseEnter(){
GetComponent(Deselect).enabled = false;
}
function OnMouseExit(){
if(boo == true){
GetComponent(Deselect).enabled = true;
}
}
function OnMouseDown(){
boo = true;
}
simples
Answer by suyujin · Jan 01, 2012 at 12:01 AM
How about something along the lines of
if(Input.GetMouseButtonDown(0) != //my game object){
boo = false; }
I would think that would let you register any click not on the game object(s).
ill give it a try as the yield time just messed everything up then eventually kept on crashing unity. to unstable.
ill post back if works
no still cant get it to work, with an update or onmousedown
How about... if(Input.Get$$anonymous$$ouseButtonDown(0) == //my game object){ boo = true; }else{ boo = false; }
ok i think after several hours i may have figured out a solid way to do it using on enter and on exit, i will post my scripts after i finish them. works so far.
basically i have one script on my object which gets disabled when i go over an object and this allows me to select it. then when i leave it enables it again so when i click anyhere else the object gets deselected.
then i put the same script on the gui and controls and then when i enter the gui it disables the script on my object and so on. i will post tommorow but any other simpler ways would be appreciated.
Answer by bificommander · Sep 05, 2012 at 01:00 PM
I realize this post is quite old already, but I just found it when looking for a similar solution, and I wanted to add my solution as an alternative suggestion for anyone else who may find this post.
I'm making a Turn Based Strategy and am currently making each grid square of the map a game object that can be selected and deselected. Since there's quite a number of these squares in a scene, I wanted to avoid any scripts that ran every update cycle for each square just to check if anything needed to be done. I think I found an adequate method.
In addition to my identical grid-square object, I make a single grid controler object. I add the script of that grid controler object as a variable to the grid-square script, so I can access its functions. To the grid-square object I add a SelectGrid() function, a DeselectGrid() function, and a function
OnMouseUpAsButton(){ GridControlerScriptVariable.selectGrid(gameObject);}
The grid controler script has a private variable created with [private var oldGrid : GameObject;] and the selectGrid() function that I call with the OnMouseUpButton() which does the following:
function selectGrid(newGrid : GameObject){
if(oldGrid)oldGrid.GetComponent(GridSquareScript).DeselectGrid();
newGrid.GetComponent(GridSquareScript).SelectGrid();
oldGrid=newGrid;
}
The if-statement in the first line is just there for the first time something is selected, when there isn't an oldGrid set yet. As far as I'm able to tell, this sollution works quite well.
Your answer
![](https://koobas.hobune.stream/wayback/20220613062543im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Mecanim Boolean 1 Answer
Shields 'always' inactive. Player object doesn't recognise if child exists(Solved) 1 Answer
Detect perimetral walls of a room 1 Answer
Spawn Object If Boolean Equals True, Boolean Set To True On Level Load 1 Answer
Error : Animator has not been initialized. UnityEngine.Animator:SetBool(String, Boolean) 1 Answer