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 BZS · Jan 04, 2014 at 05:34 PM · javascriptvariablebeginner

How do I deselect an object when another is selected?

Okay so I have two cubes and I want it so when I click the first cube it is selected and when i click the second cube it deselects the first cube and selects the second cube. How would I go about doing this? Here is the first script I tried: #pragma strict

 var selected = false;
 var clickCount = 0;
 
 function Start () {
 
 }
 
 function OnMouseOver() {
     if(Input.GetMouseButtonDown(0)){
     clickCount = clickCount + 1;
     }
     if(Input.GetMouseButtonDown(0))
     {    
         if(clickCount%2==1)
         {
         selected = true;
         Debug.Log("Grid Peice Selected");
         }
     }
     if(Input.GetMouseButtonDown(0))
     {
         if(clickCount%2==0)
         {
                     selected = false;
                     Debug.Log("Grid Peice Unselected");
         }
     }
 }

This is the second script I tried this time using raycast:

 #pragma strict
 
 var selected = false;
 
 function OnMouseDown()
 {
     
     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
 
 
     if(Input.GetMouseButtonDown(0))
     {
         if(Physics.Raycast(ray, 100))
         {
             Debug.Log("Selected");        
         }    
     
 
     }else{
     Debug.Log("Unselected");
     }
     }    
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 sath · Jan 04, 2014 at 11:32 PM 0
Share

http://answers.unity3d.com/questions/609107/how-do-i-deselect-an-object-when-another-is-select-1.html

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Josie · Jan 04, 2014 at 10:16 PM

Using the first script, i think you will need to store the first cube you select as a Boolean variable after it is selected and make it true. Then say when you click the 2nd cube something like -

 // Do this dynamically.  So once the object is clicked, it   // makes the var.  You may need to disable Pragma Strict for // this
 
 var cube1 = false;
 var cube2 = false;
 
 
 // Function with selecting the 2nd cube
 if (cube1) {
 
 // This part isn't actual code
 cube1 = selected
 
 else {
 
 cube1 = false;
 
 }
 
 if (cube2) {
 
 // This part isn't actual code again
 cube2 = selected
 
 }
 
 else {
 
 cube2 = false;
 
 
 

 
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 Josie · Jan 04, 2014 at 08:46 PM 0
Share

Scratch off

 cube1 = false;

and

 cube2 = false;


its actually

:Hypothetical Code

 cube1 = deselected

and

 cube2 = deselected
avatar image
0

Answer by valim · Jan 04, 2014 at 08:47 PM

Easiest way is to use 2 scripts.

cubeOne:

 #pragma strict
 public static var selected : boolean = false;
 function OnMouseDown () {
     cubeTwo.selected = false;
     selected = true;
 }     
 function Update() {
     if(selected){
         Debug.Log(this.gameObject.name);
     }
 }

cubeTwo:

 #pragma strict
 public static var selected : boolean = false;    
 function OnMouseDown () {
     cubeOne.selected = false;
     selected = true;
 }     
 function Update() {
     if(selected){
         Debug.Log(this.gameObject.name);
     }
 }
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

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

How do I deselect an object when another is selected? 1 Answer

Help with my code?(NullReferenceException)[CLOSED] 2 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Setting Scroll View Width GUILayout 1 Answer


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