- Home /
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.
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!");
}
}
}
"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$$
@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.
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.
Cool. If you figured it out, please close the question so others know you resolved the issue. Happy Developing.
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.
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.
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);
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.
^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.
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.