Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by SmashDivine · Jan 02, 2017 at 06:59 AM · objectclassmouseoverreference-other-object

How to determine class of object at mouse position

Hello!

I have a class ("foo" say), and when I obtain a reference to a game object at my mouse position I want to determine whether the object is of the foo class. I obtain the object with

 GameObject objAtMouse = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(mousePos), Vector2.zero).transform.gameObject;

where mousePos is the position of my mouse, and it seems to be working as intended. To test for class I have tried the following:

  1.   if(objAtMouse is foo){...} 
    
  2.   foo fooAtMouse = objAtMouse as foo;
     if(fooAtMouse){...}
    
  3.   if ((objAtMouse.GetComponent("foo") as foo) != null){...}
    
    

Option 1. was suggested here and is the only one which does not produce an error, but yields the warning

The given expression is never of the provided ('foo') type

Option 2., also suggested at the link above, yields the error

Cannot convert type 'UnityEngineGameObject' to 'foo' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion

Option 3. was suggested here and produces the error

NullReferenceException: Object reference not set to an instance of an object

This seems like a simple task, but I'm having a bit of trouble with this one. So, how can I determine the class/type of the object that my mouse is over?

Any help is greatly appreciated!

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 KoenigX3 · Jan 05, 2017 at 05:28 PM

The first line of code should work as intended. However, if the raycast returns without a gameobject, the 'objAtMouse' variable will be null, and it will produce errors.

First, you have to check whether 'objAtMouse' is an actual gameobject, then proceed with getting the right component. I assume that you are talking about 'foo' class as a script component on the gameobject (the raycasted gameobject will always have the type of 'GameObject', but the components on it will have other types).

 if(objAtMouse != null)
 {
     // GameObject found, we can proceed
 
     if(objAtMouse.GetComponent<Foo>() != null)
     {
         // The GameObject has a Foo-typed component on it
     }
 }

This part should be inserted after the raycasting line.

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 SmashDivine · Jan 08, 2017 at 03:22 PM 1
Share

Thanks a bunch @$$anonymous$$oenigX3 for the helpful answer; that did the trick! :)

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

89 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 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 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 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

NullReferenceException: Object reference not set to an instance of an object 0 Answers

Create group people, Select one or more at random. 1 Answer

List of Class With GameObject 0 Answers

Custom type Objects without hte " = new " 2 Answers

How to make objects without the new keyword 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