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
1
Question by AlwaysSunny · Mar 05, 2012 at 11:47 PM · raycastcollidercustom class

get custom class instance of collider hit by raycast?

I'm sure I've done this before, but I'm not remembering how...

I have a custom class, FooObject. Each FooObject instance has a GameObject and a BoxCollider. I'm casting a ray from one FooObject's GameObject's transfrom.position to see if any other FooObject's BoxCollider is hit.

That's all fine, but I can't remember how to access the FooObject instance of the BoxCollider I hit with my Raycast.

I need something like:

if (hit.collider != null) {FooObject whichFoo = the FooObject that owns the collider we hit}

Any takers? Thanks for your time..!

Comment
Add comment · Show 2
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 aldonaletto · Mar 06, 2012 at 12:09 AM 0
Share

The GameObject is a member of the FooObject class? And is the collider a component of this game object?

avatar image AlwaysSunny · Mar 06, 2012 at 12:48 AM 0
Share

Yes to both! :)

1 Reply

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

Answer by tingham · Mar 06, 2012 at 12:31 AM

Should be:

 // c#
 hit.collider.gameObject.GetComponent<FooObject>()  
 // js  
 hit.collider.gameObject.GetComponent(FooObject)
Comment
Add comment · Show 5 · 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 AlwaysSunny · Mar 06, 2012 at 12:47 AM 0
Share

FooObject isn't a component of a FooObject. It's its own class, defined like

 public class FooObject
 {
      GameObject myGO;
      BoxCollider myCollider;
      public FooObject(....arguments)
      {
          //instantiate stuff
      }    
 }
avatar image rutter · Mar 06, 2012 at 02:08 AM 0
Share

If each FooObject manages one GameObject, then attaching it as a component (probably inheriting from $$anonymous$$onoBehaviour) really seems like the way to go. It'll enforce that one-to-one relationship and make sure that each can access the other.

If there's some pressing reason you can't do that, I guess you're left with the option of creating and maintaining some easily accessible data structure that maps each GameObject to its respective FooObject, but... ew.

avatar image AlwaysSunny · Mar 06, 2012 at 02:20 AM 0
Share

I suppose I'm still not used to thinking with Unity's workflow. Still an XNA guy at heart, where these arbitrary relationships are there by design. Yours sounds like the superior method to begin with, so I'm satisfied with that answer if you'd like to post it as one.

$$anonymous$$any thanks. :)

avatar image tingham · Mar 06, 2012 at 02:43 AM 0
Share

The solution is that the standard way to do this is to attach your script to the game object as a component. In this scenario; my code is the appropriate way to access your script.

avatar image AlwaysSunny · Mar 06, 2012 at 04:34 AM 0
Share

Indeed, I suppose so. $$anonymous$$ight confuse someone looking unless they read these comments, but I selected it as correct. I do appreciate everyone's time and patience. :)

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Move player to specific location and avoid obstacle. 2 Answers

Im trying to create a word game with a twist 0 Answers

Raycast not detecting ANY HITS AT ALL when starting inside a collider. 0 Answers

Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer

Logic to detect objects entering between the player and camera? 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