- Home /
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.
Answer by DaveA · Mar 21, 2012 at 08:25 PM
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.
Your answer
Follow this Question
Related Questions
C#: Changing current position of an object in y. 1 Answer
Destroy at collision based on color 3 Answers
Collision between objects 2 Answers
How do I delete a gamebject that is not the one my script is in? 2 Answers
Collision with a coin 0 Answers