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 ranko · Apr 24, 2011 at 09:08 AM · gameobjectattach

Using Collider.OnCollisionEnter(Collision) without attaching the script to the concerned GameObject

Hi :),

I have question about Collisions.

Let's say i have 2 GameObjects, Cube1 with a BoxCollider (Cube1Collider) and a rigidbody and Cube2 with a BoxCollider (Cube2Collider) and a rigidbody.

Normally i attach this script on Cube1 to get information about the collision occuring between his Collider and others Colliders.

code (javascript) : function OnCollisionEnter(CollisionInfo:Collision) {

var CollidedCollider = CollisionInfo.gameObject.name; var Point:Vector3 = CollisionInfo.contacts[0].point; var Normal:Vector3 = CollisionInfo.contacts[0].normal; Debug.Log(CollisionInfo.contacts[0].thisCollider.name+" hit "+CollisionInfo.contacts[0].otherCollider.name); Debug.Log("CollidedCollider : " + CollidedCollider); Debug.Log("Point : " + Point); Debug.Log("Normal : " + Normal);

}

This is working correctly, but i don't want to manage my program this way ! I don't want to attach many scripts to many GameObject and i want to be able to test the collision between Cube1 and Cube2 in code.

For example i want to attach a main script called "Program" to an Empty GameObject and be able to test collisions between 2 GameObjects in code. But i have not managed to do this so far...

It would look like this : code javascript : function Cube1Collider.OnCollisionEnter(CollisionInfo:Collision) {

var CollidedCollider = CollisionInfo.gameObject.name; var Point:Vector3 = CollisionInfo.contacts[0].point; var Normal:Vector3 = CollisionInfo.contacts[0].normal; Debug.Log(CollisionInfo.contacts[0].thisCollider.name+" hit "+CollisionInfo.contacts[0].otherCollider.name); Debug.Log("CollidedCollider : " + CollidedCollider); Debug.Log("Point : " + Point); Debug.Log("Normal : " + Normal);

}

But it doesn't work...

Do you have any idea how to achieve this ?

Thanks

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by superpig · Apr 24, 2011 at 12:23 PM

You have to attach some kind of script to the gameObjects that participate in the collision. There is nothing built into Unity that allows objects that aren't participating in a collision to find out about them.

If you really wanted to process the collision in a central object, you could have the collision handler on each gameObject be a really simple script that just passed the data to a central object. I think this would be a bad idea though; information about the way different objects react to different collisions should be spread across the objects in question, because it reduces coupling and makes the system easier to change and extend.

Comment
Add comment · 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
0

Answer by ranko · Apr 24, 2011 at 01:04 PM

Thanks for the answer. Yes that's what i have found and i am not accustomed to attach many scripts to many entities.

Moreover i create all my entities (GameObject) in code and i am able to add colliders and rigidbodies, but i am not able to use OnCollisionEnter() because i can't specify the entity1 and entity2 to check if there is a collision...

So i guess the procedural creation of a level is not compatible with OnCollisionEnter() and therefore i will have to code my own collision system with Raycast and Spherecast...

What do you mean by : "If you really wanted to process the collision in a central object, you could have the collision handler on each gameObject be a really simple script that just passed the data to a central object."

Is this method compatible with procedural creation of entities ?

Thanks,

Comment
Add comment · 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

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

No one has followed this question yet.

Related Questions

How to add new instance of a script to a game object? 2 Answers

GO.AddComponent just adds an empty Script 1 Answer

how can I display a variable as a GUIText 5 Answers

Getting score when the two object attach each other 0 Answers

How to get a parent object after clicking the childObject 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