- Home /
How do i deselect an instance by selecting another instance of the same game Object?
Hi. I am in process of making a script which acts like a switch and applied it to an empty game object(Let's say 'GridManager').
For Example, I have created a grid of game Object 'Pivot' in a grid of 7 by 8 by creating an instance of single game object(Pivot).
I have created another script that acts as a switch that i have attached to the game object (Pivot) so that i can select and deselect it. Here is the script:
{
void Start(){
bool selected = false;
}
public void OnMouseDown(){
SwitchSelect();
}
public void SwitchSelect(){
if(selected == false && Input.GetMouseButton(0)){
selected = !selected;
print (selected);
transform.localScale = new Vector2(1.5f, 1.5f);
}
else if(selected == true && Input.GetMouseButton(0)){
selected = !selected;
print (selected);
transform.localScale = new Vector2(1f, 1f);
}
}
.....
Now here's the problem: Suppose during gameplay i want to select one instance of the game Object(Pivot). But maybe just because i changed my mind i want to select another instance by deselecting the instance i selected first. Like when i select one instance other instances get deselected automatically, i.e. i can only select one instance at a time.
I have tried several possible ways but it wont work.
Thanks Guys!! robertbu's code has helped me sorted out what i wanted to go with. Although i am going to try each of the logic provided by you all just to make sure i get to try something different in a different way. Thanks again.
Answer by robertbu · Jul 23, 2014 at 03:55 PM
There are lots of warning about using static variables on this list, but there are times when they greatly simplify a problem, and this is one of them. 'statics' can be very good at intra-class communication. For example:
using UnityEngine;
using System.Collections;
public class Example : MonoBehaviour {
private static Transform trSelect = null;
public void OnMouseDown(){
SwitchSelect();
}
public void SwitchSelect(){
if (trSelect != null) {
if (trSelect == transform) {
transform.localScale = new Vector2(1f, 1f);
trSelect = null;
}
else {
trSelect.localScale = new Vector2(1f, 1f);
trSelect = transform;
transform.localScale = new Vector2(1.5f, 1.5f);
}
}
else {
trSelect = transform;
transform.localScale = new Vector2(1.5f, 1.5f);
}
}
}
Try this instead of your script. Note how 'trSelect' always contains the transform of the selected game object (if any).
Hi Robertbu. I am trying to understand your code but could not understand how it works. Could you please help me understand how it works. Thanks.
Answer by Paprik · Jul 23, 2014 at 02:06 PM
The problem is in your design. The selection logic should not be in the objects being selected by in a manager object that controls the selection.
In that object you could have a variable:
public Object selected;
And if that's null, you'd behave as if nothing is selected. If it has a value, it's the selected object. You'll also need logic to find which object the user clicked, if I understand your intention. Look into raycasting.
Hi paprik. Thanks for the answer.
"The selection logic should not be in the objects being selected by in a manager object that controls the selection." - Does that mean that i should have all the logic assigned to a game manager that controls the required game object ins$$anonymous$$d of the object itself?
The logic you described does state what i am expecting to be looking for. I will take that into consideration. Thanks.
Answer by Xtro · Jul 23, 2014 at 03:43 PM
General selection methods...
1) If you have a "bool selected" property on each selectable objects: you can have multiple selection but it's harder to find the selected objects fast and harder to deselect older objects.
2) If you have only one global "GameObject selected" variable: finding the selected object and deselecting the old object is instant but you can't have multiple selection.
3) if you have an array of selected variables like "List<GameObject> SelectedObjects" : You can find or deselect the selected objects almost instant (faster than first method, slower than second method) and you can still have multiple selection.