- Home /
Help with selecting and deselecting an object?
Hi! I am trying to make it so when I click a cube it is selected(I already scripted this part) but when i click outside of the cube it deselects it. How would i go about doing this? I don't need code i just need an explanation. Thanks in advanced.
Answer by NightmarexGR · Jan 04, 2014 at 02:11 AM
It would me helpful if u posted the code and just have me modify it for you but lets do it your way. Well i think there has to be an "else" statement inside your code, try deleting it or making it so if the place you clicked doesnt have any object or is not a cube -> do nothing instead of making your variable null.
Hope it helps.
~NightmarexGR
EDIT : (Cause site didnt let me to multiple answer)
Hmm ok so what you want is to select only the first cube and if the first cube is selected and u click anywhere else from that cube even if u click another cube to have it deselected ? Ok for this one you can do this:
var selected = false;
var MyCube : Transform;
function Update () {
if (MyCube != null) {
selected = true;
} else { selected = false; }
}
function OnMouseDown() {
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetMouseButtonDown(0)) {
if(Physics.Raycast(ray, 100)) {
if (hit.transform.name == "TheCubeNameIwant"){
if (MyCube == null) { MyCube = hit.transform; }
} else {
if (hit.transform != MyCube) { MyCube = null; }
}
}
}
}
//(Untested Code)
~NightmarexGR
The problem is I have more than one cube so if I select a different cube it doesn't deselect the first one. I can't post my script right now because I don't have access to my computer.
The reason it would help to see your code is so that we can understand what you mean by 'selected' , and how the various scripts interact. For instance, you may have it all happening in one script, or you may have a second script that keeps track of what's selected by listening to signals from the 'selectable' script.
Either way, without being aware of the idiocyncracies of your implementation, it is difficult to offer a solution. Really, it's pretty trivial, and if you've got the selection part working I'm honestly wondering how you did it that doesn't make it obvious to you how you'd deselect something.
Is it possible that you are storing the "Selected Object" in an array cause it sounds like thats what you do. But either way as mentioned before and as "iwaldrop" said its difficult to offer a solution whithout enought information about the problem or beeing able to see the code.
Here is my code:
#pragma strict
var selected = false;
function On$$anonymous$$ouseDown()
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.Get$$anonymous$$ouseButtonDown(0))
{
if(Physics.Raycast(ray, 100))
{
Debug.Log("Selected");
}
}else{
Debug.Log("Unselected");
}
}
That looks good. Ok. So you need to have some way of differentiating between a selectable and a non selectable object (I.e. a unit and the ground). The easiest is a physics layer for two reasons. One, it's easily setup in the inspector, and Two, your raycast can ignore layers it doesn't care about, only targeting selectable objects.
If this were possible in your project (it may not be, depending on whether you're already using the physic layers to accomplish something else entirely different), then you could use tags, or monobehaviours to denote selectable things.
Either way, you'd do a quick test to deter$$anonymous$$e if you've hit something that's selectable, and if so store it. If you haven't hit a selectable object, you'd clear the field, effectively deselecting the last selection.
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Scripting Tut. 1 Answer
Weird Code Error Message 2 Answers