• 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 /
This question was closed Jun 23, 2015 at 12:55 PM by carlqwe for the following reason:

Duplicate Question

avatar image
-1
Question by carlqwe · Jun 23, 2015 at 11:24 AM · onmousedown

OnMouseDown, How to detect if the mouse clicks outside of the collider

Hello i have a major problem, im creating a strategy game and i have it so if i press on my "worker" then he will be "selected" and a blue ring shows under his feet, but i want it so if im clicking anywhere but now on the worker, then the ring will disapear. Any ideas? here is my script: (JS)

 #pragma strict
 
 var SelectedRing : GameObject;
 var Selected : boolean = false;
 
 var showGUI : boolean = false;
 
 var WorkerSpawn : GameObject;
 var Worker : GameObject;
 
 function Start ()
 {
     SelectedRing.SetActive(false);
 }
 
 function OnMouseDown ()
 {
     Selected = true;
     SelectedRing.SetActive(true);
 }
 
 function OnGUI ()
 {
     if(showGUI == true)
     {
         if(GUI.Button(new Rect( 100, 50, 100, 100), "Spawn Worker"))
         {
             Instantiate(Worker, WorkerSpawn.transform.position, Quaternion.identity);
         }
     }
 }
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 meat5000 ♦ · Jun 23, 2015 at 11:58 AM 2
Share

Its always worth adding in a little info about what you've tried etc. The more you appear to have tried to help yourself, the more people will be willing to help you :)

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by DoomTay · Jun 23, 2015 at 12:04 PM

You could try setting up a ray

 function OnMouseDown
 {
     var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
 
     if (Physics.Raycast(ray) && hit.transform.gameObject == Worker) {
      Selected = true;
      SelectedRing.SetActive(true);
    }
    else
     //Clicked on something else

 }

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 carlqwe · Jun 23, 2015 at 12:26 PM 0
Share

Im very thankful for what you did for me, but still don't understand what the error is, "NullReferenceException: Object reference not set to an instance of an object worker_Controller.On$$anonymous$$ouseDown () (at Assets/Scripts/worker_Controller.js:21)"

here is my code:

 #pragma strict
 
 var SelectedRing : GameObject;
 var Selected : boolean = false;
 
 var showGUI : boolean = false;
 
 var WorkerSpawn : GameObject;
 var Worker : GameObject;
 
 function Start ()
 {
     SelectedRing.SetActive(false);
 }
 
 function On$$anonymous$$ouseDown ()
 {
  var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  var hit : RaycastHit;
  
      if (Physics.Raycast(ray) && hit.transform.gameObject == Worker)
      {
          Selected = true; SelectedRing.SetActive(true);
     }
      else
     {
          //Clicked on something else
      }
 }
 
 function OnGUI ()
 {
     if(showGUI == true)
     {
         if(GUI.Button(new Rect( 100, 50, 100, 100), "Spawn Worker"))
         {
             Instantiate(Worker, WorkerSpawn.transform.position, Quaternion.identity);
         }
     }
 }
avatar image DoomTay · Jun 23, 2015 at 12:53 PM 0
Share

Whoops. Try replacing Physics.Raycast(ray) with Physics.Raycast(ray,hit)

avatar image carlqwe · Jun 24, 2015 at 10:56 AM 0
Share

THAN$$anonymous$$ YOU!

Just changed the code a little bit and now it works!

 #pragma strict
 
 var SelectedRing : GameObject;
 var Selected : boolean = false;
 
 var showGUI : boolean = false;
 
 var WorkerSpawn : GameObject;
 var Worker : GameObject;
 
 var lastHit: GameObject; // $$anonymous$$eep track of last object clicked
 
 function Update ()
  {
   var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
   var hit : RaycastHit;
   
       if(Input.Get$$anonymous$$ouseButtonDown(0))
       {
          if (Physics.Raycast(ray,hit) && hit.transform.gameObject == Worker)
          {
                    Selected = true;
               SelectedRing.SetActive(true);
          }
           else
          {
               Selected = false;
               SelectedRing.SetActive(false);
          }
       }
  }
 
 function Start ()
 {
     SelectedRing.SetActive(false);
 }
 
 function OnGUI ()
 {
     if(showGUI == true)
     {
         if(GUI.Button(new Rect( 100, 50, 100, 100), "Spawn Worker"))
         {
             Instantiate(Worker, WorkerSpawn.transform.position, Quaternion.identity);
         }
     }
 }

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

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

Related Questions

OnMouseDown with 360 controller unity? 1 Answer

GameObject Array 1 Answer

Increase mouseclick area size for OnMouseDown() - For Mouse Shooting Game (Open to more ideas) 2 Answers

Click on a gameObject to show/hide a GUI box help C# 1 Answer

Collider priority with onMouseDown() 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