Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
4
Question by oliver-jones · Nov 21, 2010 at 01:38 AM · mousemouseclickclickable

How To Perform A Mouse Click On Game Object

Hello,

I would like the ability to click on an object in the game play.

Eg: user clicks on cube in the game, a dialog window then pops up, or an event is triggered.

I have no person controls or anything, just a camera.

--- Snippet of Scrrpt -----

function Update () 
{
    if(Input.GetMouseButtonDown (0) && Clicked == false)
    {

This works, but I can click anywhere on the screen....

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

5 Replies

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

Answer by Uriel_96 · Nov 21, 2010 at 02:12 AM

try this

if ( Input.GetMouseButtonDown(0)){
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
var select = GameObject.FindWithTag("select").transform;
if (Physics.Raycast (ray, hit, 100.0)){
select.tag = "none";
hit.collider.transform.tag = "select";
}
}
Comment
Add comment · Show 4 · 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 oliver-jones · Nov 21, 2010 at 02:40 AM 0
Share

Okay, but what about when I want to deselect it or unclick it ... Thanks

avatar image Uriel_96 · Nov 21, 2010 at 03:33 AM 0
Share

good point, Im not completly sure but I will try to put an else something like this maybe: if (Physics.Raycast (ray, hit, 100.0)){}else{//Do what you want}

avatar image TheJables · Aug 13, 2012 at 04:10 PM 0
Share

I've been looking for a way to detect if you've clicked on a game object and if so, destroy it. However, I'm getting some errors just trying to implicitly declare var hit and var ray to the different RaycastHit and Ray structs. I assume that there's no additional using statements needed here? Is there something I might be missing? I've copied the code above as is but it doesn't seem to like the ':' after hit and ray.

avatar image kamal2013thapa · Apr 06, 2013 at 12:19 PM 0
Share

I am also facing same problem.When I mouse over on the object it move.But i want to move, when left mouse clicked.

kamal thapa april 6th, 2013

avatar image
13

Answer by JakeElliott · Nov 21, 2010 at 02:21 AM

You can use the OnMouseDown function in a script component attached to your cube, as long as it has a collider: http://unity3d.com/support/documentation/ScriptReference/MonoBehaviour.OnMouseDown.html

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 Screenhog · Aug 13, 2012 at 04:37 PM 1
Share

This is true, but beware that On$$anonymous$$ouseDown (and similar functions) don't work on mobile devices. Depending on your needs, this might not be a problem, but I thought it worth mentioning anyway.

avatar image
1

Answer by jharri30 · Jan 17, 2014 at 03:08 AM

I was having some issues with Uriel's code, so I tweaked it a little to simplify it. Here is the solution that worked for me.

I created a cube which had a "Box Collider" on it by default and renamed it to "targetArea". I then added a script to it with the following update statement...

 function Update ()
 {
     if ( Input.GetMouseButtonDown(0))
     {
         var hit : RaycastHit;
         var ray : Ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         if (Physics.Raycast (ray, hit, 100.0))
         {  
             Destroy(GameObject.Find("targetArea"));
         }
     }
 }
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 TSCGCobra043 · Apr 04, 2016 at 05:12 PM 0
Share

do you know how to do this but ins$$anonymous$$d when clicked will load a new scene?

avatar image derlin TSCGCobra043 · Sep 10, 2016 at 02:10 PM 0
Share

Just replace Destroy(...); with the following sceneloading function.

using UnityEngine.Scene$$anonymous$$anagement; //add to top of script

Scene$$anonymous$$anager.LoadScene(string);

avatar image Stevens-R-Miller · Dec 21, 2017 at 11:06 PM 0
Share

Long time later, but I am a bit confused by this code. While it tests to see if the ray hit anything, it doesn't seem to query for what it hit. If you only have one collider, then a hit has to be on that collider. But what if you have two or more? How do you know which one you hit?

avatar image
1

Answer by OgiJr · Dec 26, 2015 at 11:15 AM

Void OnMouseClick() { function }

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
avatar image
0

Answer by AlisVitae · Jun 21, 2015 at 06:02 PM

Look at our tutorial! https://www.youtube.com/watch?v=HR_88quPMog

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

12 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

Related Questions

Mouse movement and click effect 1 Answer

Raycast hit not detected on cube 1 Answer

Map mouse click state to a Joystick button? 0 Answers

Stop object rotation 1 Answer

"Working with moving and mouse touch to set coordinates" 0 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