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 01:51 AM · javascriptbeginnerclick objects

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.

Comment
Add comment
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

1 Reply

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

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

Comment
Add comment · Show 5 · 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 BZS · Jan 04, 2014 at 02:23 AM 0
Share

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.

avatar image iwaldrop · Jan 04, 2014 at 06:43 AM 2
Share

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.

avatar image NightmarexGR · Jan 04, 2014 at 02:13 PM 0
Share

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.

avatar image BZS · Jan 04, 2014 at 02:46 PM 0
Share

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");
     }
     }    
avatar image iwaldrop · Jan 04, 2014 at 06:25 PM 0
Share

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

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

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


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