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 carlqwe · Jun 23, 2015 at 03:49 PM · raycast

OnMouseDown, Raycast, Error

Hello here i have a part of a script that reads, if i press on the gameobject then a boolean will be activated and an object will be activated, but if i click outside of the gameobject then the boolean will be set to false, and the object will be set to false, but i get this error when i click on the gameobject:

NullReferenceException: Object reference not set to an instance of an object worker_Controller.OnMouseDown () (at Assets/Scripts/worker_Controller.js:21)

Any ideas? Thanks :) here is my code:

 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
         {
          Selected = false;
          SelectedRing.SetActive(false);
          }
      
     }
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 Ibzy · Jun 23, 2015 at 03:58 PM 0
Share

Is SelectedRing predefined? Guessing here, but do you have it public and need to assign the gameobject in the inspector?

3 Replies

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

Answer by TrainWrek · Jun 24, 2015 at 12:05 AM

Your hit variable is never assigned to so it will always remain null. You should use "Physics.Raycast(ray, out hit);" instead.

 function OnMouseDown () {
     var ray: Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
     var hit : RaycastHit;
 
      if (Physics.Raycast(ray, out hit) && hit.transform.gameObject == "Worker") {
         Selected = true;
         SelectedRing.SetActive(true);
     }
     else {
         Selected = false;
         SelectedRing.SetActive(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 carlqwe · Jun 24, 2015 at 09:02 AM 0
Share

I get a lot of errors in row 21, 25, & 31. It says that the code is missing some symbols

avatar image
1

Answer by Ibzy · Jun 23, 2015 at 06:53 PM

This is hard to troubleshoot without knowing the full setup of your scene: is the above script attached to the object you want to select? Theoretically you won't need to fire a Raycast as the OnMouseDown has already detected a click on the object in question.

Also, your check of hit.transform.gameObject == "Worker" will return false. Perhaps you mean hit.transform.gameObject.name == "Worker" or hit.transform.gameObject.tag == "Worker"?

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

Well the script that is shown above is attatched to the "Worker" and the gameObject is named "Worker" the gameobject has also an boxcollider, and i can click on the worker, but when i do so the error apears

Here is a picture of my scene:

http://imgur.com/x7U8yeg

avatar image Ibzy · Jun 23, 2015 at 10:01 PM 0
Share

Looks to me like you don't need the Raycast for a start - On$$anonymous$$ouseDown will indicate you have clicked on him. I assume what you are trying to achieve is to highlight the worker you have selected with a blue ring, and then if you click not on him the ring disappears?

avatar image carlqwe · Jun 23, 2015 at 10:43 PM 0
Share

yes thats what i want, but i still need some kind of code to do that

avatar image Ibzy · Jun 23, 2015 at 11:50 PM 1
Share

Ok, cool. So I understand what you're trying to do.

2 options: 1. try what you have but remove the raycast, the game already knows you clicked on that worker as it's his script that's being activated. 2. Have a gamemaster object with a script that checks for mouse clicks on Update() and does the raycast there. from here you can have it activate the ring of the selected object IF it is a worker, otherwise clear selection.

avatar image
0

Answer by adriandevera · Jun 23, 2015 at 03:58 PM

You simply need to have a collider on that object. Without one theres nothing for the raycast to collide with and check in with the bool if it did.

You will need to add it in the inspector or you can do it through code if your doing it on runtime.

SelectedRing.AddComponent();

You can use other colliders as well such as MeshCollider, SphereCollider etc.

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

I have a collider on the object that this script is attatched to

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

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

Mouse Click + Raycast + Colliders 2 Answers

Realistic Bow and Arrow Physics? 2 Answers

GraphicRaycaster not detecting UI elements 0 Answers

Raycast from a 3rdp controller 1 Answer

Raycast on ui elements not working? 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