Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Rudraksha · Jul 23, 2014 at 01:56 PM · gridinstanceswitchselectiondeselect

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rudraksha · Jul 23, 2014 at 06:46 PM 0
Share

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.

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rudraksha · Jul 25, 2014 at 09:18 AM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Rudraksha · Jul 23, 2014 at 03:32 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Selecting imported geometry selects ALL the geometry and uncontrollable for multiple objects 1 Answer

How do you check one box in a GUI.SelectionGrid? 1 Answer

What is wrong with my code? 1 Answer

Unity5 UI - How to trigger button click event while preventing menu item deselect event? 1 Answer

Deselect object when another instance is selected 3 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges