Hiya, I am trying to figure out how to destroy a light on collision, basically its a ball with a light attached to it, and im not sure how to write a script for the light to destroy once it collides with anything. Anyone got any ideas?
112 posts and you haven’t yet read the docs? :shock:
Well im not sure what im doing wrong but
var ToDestroy : GameObject ;
function OnCollisionEnter(collision : Collision) {
for (var contact : ContactPoint in collision.contacts) {
Destroy (gameObject);
}
}
It should work, ive attached the light to ToDestroy.
What did i do wrong?
I don’t really know what the contact point part is for, and I’m assuming that you want to destroy “toDestroy” (which can’t have a capital letter first), so I think this would do it
var toDestroy : GameObject ;
function OnCollisionEnter(collision : Collision) {
Destroy (toDestroy);
}
that would destroy the light(if your light is a seperate gameObject) when whatever this script is attached to collides. If you want to destroy the light component of the gameObject, it would be
function OnCollisionEnter(collision : Collision) {
Destroy (light);
}
if you’re wondering what you did wrong, it would be the variable, the for loop which is useless, and the fact that Destroy(gameObject) destroys whatever this script is attached to.
hurray, thanks btm, now the light destroys on collsion, thanks bro ![]()
You’re attaching this script to the object that the lightball is colliding with, correct? Make sure the lightball thingy has a tag of lightball assigned. Then try the following.
Function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.CompareTag ("Lightball")) {
Destroy (collision.gameObject);
}
}
I’m not sure why you were collecting the contact information. If you are simply destroying the object why do you need that information? You spawning a prefab at the collision point or something?
If you don’t really need the detailed collision information a trigger would be more performant. In fact if you look at the docs for triggers.
// Destroy everything that enters the trigger
function OnTriggerEnter (other : Collider) {
Destroy(other.gameObject);
}
It’s script does exactly what you want.
Yeah i get it now, the script turned out to be really simple, just
var todestroy : GameObject ;
function OnCollisionEnter(collision : Collision) {
Destroy (todestroy);
}
Thanks for your help (:
Well the only problem with that script, is that the lightball thingy will be destroyed no matter what collides with your target. That’s why I compared tags in the example I gave you.
If your lightball is the only thing moving about, then that’s not really an issue.
Thats the beauty of that script, its exactly what i wanted, to be destroyed if it hits any single thing, but i will definetly be using what you said for other occasions if ever needed ![]()