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 Captain Panic · May 11, 2013 at 08:24 PM · javascriptgameobject

How Do You Select A Game Object By Using the Left Mouse Button?

Hey everyone I am trying to make a game but one of the main parts to it is selecting game objects with the left mouse button. I was wondering if anyone knew the java script to allow you to click on a game object and the game object change color or something as a result. Also just to mention I have very little experience with this and have done little to no coding/scripting or whatever it is called. I have checked google and the results I have found have been of little help due to my inexperience. Also does anyone know how to make this work for prefab or instantiated objects too? Any help is appreciated.

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 Brahim113 · May 11, 2013 at 08:33 PM 0
Share

I dont use javascript. But a good tip is to try raycasting. You could easily get the gameobject by doing so. :)

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by DaveA · May 11, 2013 at 08:45 PM

https://www.google.com/search?q=Select+A+Game+Object+By+Using+the+Left+Mouse+Button

Does anybody read the docs? http://docs.unity3d.com/Documentation/ScriptReference/RaycastHit-collider.html

Comment
Add comment · Show 2 · 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 artie · May 11, 2013 at 09:06 PM 0
Share

Ha the code is right there

avatar image Captain Panic · May 11, 2013 at 09:08 PM 0
Share

Im certain it is but im new to this and have practically no idea what most of it means, but thank you for the help none the less.

avatar image
0

Answer by aldonaletto · May 11, 2013 at 09:01 PM

A common method is to use Raycast in a camera script, and change the hit object properties - like this:

 var hitTransform: Transform; // transform of currently selected object

 function Update(){
   if (Input.GetMouseButtonDown(0)){ // if left mouse pressed...
     // create ray passing through the mouse pointer:
     var ray = camera.ScreenPointToRay(Input.mousePosition);
     var hit: RaycastHit;
     if (Physics.Raycast(ray, hit)){ // if something was hit...
       hitTransform = hit.transform; // get a reference to its transform
       //
       // and do whatever you want with this object here
       //
     }
   }
 }

The variable hitTransform holds the transform of the last object clicked.

Comment
Add comment · Show 3 · 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 Captain Panic · May 11, 2013 at 09:29 PM 0
Share

Okay thank you so much this is exactly what I needed.

avatar image Captain Panic · May 11, 2013 at 09:41 PM 0
Share

I have an issue though because the object I want to select is going to be a prefab and this works fine for selecting a game objects but how can I select prefabs individually in this method?

avatar image aldonaletto · May 12, 2013 at 05:03 PM 0
Share

Well, prefab selection is editor script stuff: you must save your script in Assets/Editor and use the Editor classes - there's a Selection class that lists all selected objects. I played too little with Editor scripts, but suppose that you don't have to worry about the selection code - just inspect the Selection class to know which objects are selected.

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

18 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

Related Questions

Multiple Cars not working 1 Answer

How to tell if 2 blocks are next to each other in a 2d game? 1 Answer

Hit any gameobject, animation will play? 1 Answer

What variables can i declare? 1 Answer

Destroy GameObject A or B 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