destroy object on collision

trying to make some spheres I’m throwing disappear after a few seconds. i know i have some syntax wrong and i tried looking through reference but I’m not understanding whats wrong. heres my code:

var speed = 3.0;
var boxy:Transform;
/////
var player : GameObject;
player = gameObject.tag =("player");
///////////
var balls : GameObject;
balls = gameObject.tag =("balls");
/////////
var floor: GameObject;
floor = gameObject.tag =("floor");
/////////////////


function Update() { 

if(Input.GetButtonDown("Fire1"))
{

var crate = Instantiate (boxy, transform.position, Quaternion.identity);

crate.rigidbody.AddForce(transform.forward * 1000);



}}
function OnCollisionEnter (hit : Collision)

{ ///you are colliding

Debug.Log("CollidingWithAnything");

if(hit.gameObject.floor == hit.gameObject.balls){ //now colliding with certain object

Debug.Log("Colliding With Certain"); 
Destroy (balls, 2);

}

}

You know, you can format this code by selecting it, then hit the 010/101 button above the text area. Please do.

1 Answer

1

if(hit.gameObject.floor == hit.gameObject.balls)

gameObject is a GameObject, and if you look in the Scripting Reference, you’ll see that it contains neither ‘floor’ nor ‘balls’. Generally folks use Tags to identify what hits Colliders.

Something like

if (hit.gameObject.tag == “balls”)

Assuming you create a ‘balls’ tag and assign it to your ball objects

Destroy(gameObject.tag == "balls", 4); no luck if (hit.gameObject.tag == "balls") Destroy (hit.gameObject, 4);

do i declare a var for GameObject? whats it assigned? all i am understanding is that it is some floating syntax without an identity. I'm still unclear.

Sorry, edited comment to fix it. If you use hit.gameObject it should work.