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 zmar0519 · Mar 26, 2011 at 03:02 AM · collisioncollider

How to check if an object has collided with a specified object from a script

Hello, everybody. I am trying to make a script that executes a command when object A collides with object B. The script, however, needs to be on object C. Any pointers on how to do this would be great.

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 Statement · Mar 27, 2011 at 01:13 PM 0
Share

This approach seem a bit odd. What are you trying to accomplish? Perhaps there's a better way?

1 Reply

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

Answer by Meltdown · Mar 26, 2011 at 06:21 AM

1.) Create a script called ObjectCScript, and attach it to object C.

2.) Create a function on ObjectCScript, called ABCollided()

3.) Create a script called ObjectAScript, and attach it to object A.

4.) Add the following lines of code to ObjectAScript...

public ObjectCScript objectCScript; // Set this in the inspector

void OnCollisionEnter(Collision theCollision) { if(theCollision.gameObject.name == "ObjectB") // If A collides with B { objectCScript = objectCScript.GetComponent<ObjectCScript>(); objectCScript.ABCollided(); // Call the ABCollided function on ObjectB

     }
 }

You can also use SendMessage to call a script method on another object.

Here is a JavaScript example

Add this script to ObjectC..

function ABCollided()
{
  Debug.Log("A collided with B");
}

Then add this script to ObjectA...

function OnCollisionEnter(collision : Collision) {
 if(collision.gameObject.name == "ObjectB") // If A collides with B
{
  gameObject.Find("ObjectC").SendMessage("ABCollided");
 }
}
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 zmar0519 · Mar 26, 2011 at 10:50 PM 0
Share

Thanks for the response. Would it be possible to get that code in JS?

avatar image Meltdown · Mar 27, 2011 at 12:23 PM 0
Share

I have added the Javascript way of doing it, using the Send$$anonymous$$essage command. Please mark my solution as the answer if it helped. Thanks

avatar image impurekind · Aug 21, 2018 at 03:03 PM 0
Share

How can I get the following to work properly:

 void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "Floor") // So the bullet is on the floor and not sitting on some crate or whatever
         {
             if (timer > 300f) // So the bullet has almost certainly come to a complete standstill or isn't moving enough to notice
             {
                 Destroy(bulletCollider);
                 Destroy(bulletRigidbody);
 
                 Debug.Log("Bullet is on floor with collision and rigidbody deactivated");
             }
         }
     }

If I set the timer to a low value then everything works and I see the debug text because the collision has happened after the timer has reached the specified value. But if I set the timer to a higher value, where the bullet might have bounced a few times and come to rest then it fails and the debug text doesn't show?

Basically, I want the bullet shell to turn off some of its components after a certain time--so the game isn't processing things like colliders and rigidbodies unnecessarily--but only so long as it's already on the floor. And after that is done I don't want to run the code again. This is all just so I can have a bunch of bullet shells laying around without taking up any real resources. But, I do actually want them to remain there rather than just disappear or fade out after a few seconds as happens in most fps games.

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

1 Person is following this question.

avatar image

Related Questions

super mario pipe physics? 1 Answer

Trouble with OnCollisionEnter and Exit 1 Answer

destroy touch the screen on gameobject in camera 0 Answers

Audio play when object hit collision. 2 Answers

Toggle a script/key on when collide with object. 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