- Home /
Object destroys on collision script
So I want my "Gayballs" to disappear when they hit something and I've made some code here and it doesn't give me any errors but doesn't seem to be working.There's more code where the Gayball prefab is instantiated. And I put this script on my Gayball prefab. Could anyone see what's wrong here? Thanks in advance :)
var GayballPrefab : Transform; var wall : String = "Cube";
function OnCollisionEnter(collision : Collision) { if (collision.gameObject.GayballPrefab == wall) { Destroy(GayballPrefab); } }
function Update () {
Answer by aldonaletto · Mar 03, 2012 at 10:55 PM
No surprise - this is completely wrong! Forget about the GayballPrefab: you can identify the hit object by name or tag - gameObject.GayballPrefab doesn't exist!
function OnCollisionEnter(collision : Collision) { if (collision.gameObject.name == "Wall"){ // if the hit object's name is Wall... Destroy(collision.gameObject); // destroy it } }
Answer by mattt111 · Jan 04, 2013 at 11:25 PM
function OnCollisionEnter(collision : Collision) {
if (collision.gameObject.name == "Wall"){ // if the hit object's name is Wall...
Destroy(gameObject); // destroy it
}
}
otherwise it would destroy the wall :P
PS: it would be better if u create a tag , just change collision.gameObject.name to collision.gameObject.tag