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 Hugs Are Drugs · Aug 23, 2013 at 02:30 AM · c#collideroncollisionenter

How to choose what collider OnColliderEnter works with?

simple question, i want to choose what collider is used in OnCollisionEnter, i have a gameObject variable that i want to choose from. i just really don't want to have another script attached to the object for certain reasons.

Comment
Add comment · Show 7
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 clunk47 · Aug 23, 2013 at 06:02 AM 1
Share
 using UnityEngine;
 using System.Collections;
  
 public class Example : $$anonymous$$onoBehaviour 
 {
     public Collider collider;
     
     void OnCollisionEnter(Collision c)
     {
         if(c.collider == collider)
         {
             print (c.collider.gameObject.name + " has been hit!");    
         }
     }
 }
avatar image Hugs Are Drugs clunk47 · Aug 23, 2013 at 06:25 AM 0
Share

As stated before, this is not what I want

avatar image clunk47 clunk47 · Aug 23, 2013 at 06:32 AM 2
Share

"I want to choose what collider is used in OnCollisionEnter". Your exact words. This script allows you to drag and drop the a GameObject w/ a Collider Component onto the script attached to your collider detecting collisions. This will only detect with the chosen collider. Don't just sit here and say "this is not what I want". Ins$$anonymous$$d, TELL US WHAT YOU WANT, $$anonymous$$A$$anonymous$$E YOUR QUESTION $$anonymous$$A$$anonymous$$E SENS$$anonymous$$

avatar image robertbu clunk47 · Aug 23, 2013 at 06:55 AM 1
Share

@Clunk47 - what he wants is to have an OnCollisionEnter-Type-Functionality that processes collisions on other game objects...game objects without scripts.

@Hugs Are Drugs - To the best of my knowledge it cannot be done. You could use C# Events and Delegates to centralize your processing of collisions.

Show more comments
avatar image Hugs Are Drugs · Aug 23, 2013 at 07:34 AM 0
Share

He didn't make sense of it, I did in a comment if you'd scroll down, XD.

Anyway, thanks. I did what I want anyway.

avatar image clunk47 · Aug 23, 2013 at 04:15 PM 0
Share

Cool. If you figured it out, please close the question so others know you resolved the issue. Happy Developing.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SanX91 · Aug 23, 2013 at 04:18 AM

You can easily do it by assigning a tag to the gameobject. Say the tag is "MyObject".

Example : -

void OnCollisionEnter(Collision _collision) {

if(_collision.collider.tag == "MyObject") {

       //Something happens;

}

}

For collision detection make sure the object on which the script is attached or the object to be collided with has a rigidbody attached to it.

Comment
Add comment · Show 6 · 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 Hugs Are Drugs · Aug 23, 2013 at 04:42 AM 0
Share

This is not I want at all, you have misunderstood the question.

I'll re-explain in a different way, I want to essentially attach a script to a gameObject while it's being instantiated, but then removed if something happens in that script.

This isn't how I explained it in the original question, but this method would also suit my needs.

What I meant in the original question is, can I have an OnCollisionEnter and not have it execute on the object I attach it too, but an object I set it to execute on? I don't want to actually attach the script to the object that it will execute on.

avatar image SanX91 · Aug 23, 2013 at 04:57 AM 0
Share

That's not possible with OnCollisionEnter. It's strictly meant for the monobehaviour it is attached to.. Why do u hesitate to attach it to any gameobject?? It's not like Update(), it does not run each frame...

Btw,

you can attach a script with AddComponent;

and for destroying use Destroy(TheScript);

avatar image Hugs Are Drugs · Aug 23, 2013 at 06:31 AM 0
Share

I did an alternative to what I want to achieve, and about it executing every frame, in this case I almost want it to.

Here's my code:

WW void OnCollisionEnter (collision touch) { If (placed == gameObject) { Destroy (gameObject); } }WW

$$anonymous$$aybe that will make you understand what I wanted to do.

Is there any better way? There's sort of a small delay for it to execute which I don't want, but isn't too big if a deal.

avatar image Sajidfarooq · Aug 23, 2013 at 04:55 PM 1
Share

^All that achieves is that it destroys the gameObject when "placed" is the same as gameObject. No idea what "placed" is. The question is still as vague as ever.

avatar image Hugs Are Drugs · Aug 23, 2013 at 07:37 PM 0
Share

Yes, that's what I want it to do.

I built a script that will place down certain objects where you click, and placed is assigned to the last one you placed down and is reassigned every time you place down a new one, what this does is it checks to make sure that the gameObject placed isn't touching anything that I don't want it to touch.

That if statement is fully what I used, I have it also check make sure that it isn't detecting collisions with something that its fine to collide with, and several variables and reset. The reason I didn't eant to put the script on each of the instantiated prefab is that if two them of are touching eachother, both of them are destroyed, and if I did something to make it so only one is destroyed then the variables that are modified still got incremented/decremented which I don't want to happen.

This script is attached to every one of the instantiated prefabs, but only executes on the mist recently placed one.

I hope this clears things up, I should've just explained this in the first place.

Show more comments

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

18 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

Related Questions

How to reference another object's collider for OnCollisionEnter. 1 Answer

How to Give OnCollisionEnter Priority for 2 prefabs ? 1 Answer

Multiple Cars not working 1 Answer

How to detect collisions in C# 2 Answers

How to make gameObject make only one collision 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