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 MárcioHCG · Mar 16, 2013 at 08:36 PM · collisioncolliderobject

Get instance of the collider object

I'm new to Unity dev. So I have the character and the enemy, two objects in game. When one attacks other I can get it on event OnCollisionEnter. Great but my problem is that I don't know how I get the instance of the object that makes that collision.

Example: My character has attacked by the enemy. I can get the tag by collision.collider.tag but I want to access it's instance for some properties and methods I have to call.

Note: I'm using C#.

Comment
Add comment · Show 4
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 robertbu · Mar 16, 2013 at 08:41 PM 0
Share

collision.collider.gameObject - is this what you are looking for?

avatar image whydoidoit · Mar 16, 2013 at 08:43 PM 1
Share

Or collision.collider.GetComponent< OneOfYourScripts>().someVariable etc...

avatar image MárcioHCG · Mar 17, 2013 at 12:27 PM 0
Share

@whydoidoit No man, this return a null reference exception...

avatar image MárcioHCG · Mar 17, 2013 at 12:29 PM 0
Share

@robertbu collision.collider.gameObject will give me an object of gameObject type. I want an instance of my own object.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by robertbu · Mar 17, 2013 at 04:53 PM

I'm assuming you want access to the component on the other game object. That is access to the public instance variables and public methods of the class you wrote and attached to the other game object. @whydoidoit's method above will work assuming the object you collided with has that script. If it does not, you will get a null reference. Take it a piece at a time to see what is going on. Substitute whatever tag you have for "Enemy" (or you can check the name). Or you can just do he GetComponent() and check to see that the return is not null.

 void OnCollision(Collision collision) {
     Debug.Log("Name: "+collision.collider.name+" Tag: "+collision.collider.tag);
     
     if (collision.collider.tag == "Enemy") {
         MyScript ms = collision.collider.GetComponent();
         if (ms == null)
             Debug.Log ("Script MyScript not attached to object");
         else
             ms.myVariable++;
     }
 }
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 MárcioHCG · Mar 17, 2013 at 07:29 PM 0
Share

@robertbu Sorry but compiler doesn't accepted the use of GetComponent() without parameters. It throws this error: CS0411: The type arguments for method `UnityEngine.Component.GetComponent()' cannot be inferred from the usage. Try specifying the type arguments explicitly

avatar image robertbu · Mar 17, 2013 at 08:36 PM 0
Share

For some reason, sometimes there is trouble using angle brackets in comments and everything between the brackets is removed. Replace the {} with angle brackets in the following text:

 $$anonymous$$yScript ms = collision.collider.GetComponent{$$anonymous$$yScript}();

See @whydoidoit's example above as another example.

avatar image MárcioHCG · Mar 17, 2013 at 10:38 PM 0
Share

Sorry but I never saw it on C#. That even compiles...

avatar image whydoidoit · Mar 17, 2013 at 10:41 PM 0
Share

@robertbu include a space after every opening < angle bracket> and the parser will stop thinking it's an HT$$anonymous$$L command :)

avatar image robertbu · Mar 18, 2013 at 12:09 AM 0
Share

@whydoidoit - got it. $$anonymous$$akes sense. Thanks!

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

11 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

Related Questions

Simple two cube collision is not good? 0 Answers

Picked up objects passes through objects 2 Answers

Colliders coming away from their parent object? 1 Answer

Can an object to transfer to another as it increases your speed? 1 Answer

how to check if one object is colliding with another 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