i have my cube and walk around the plane…ok…collide with another cube with rigidbody…now if i want destroy my second cube when my first cube touch it, how i can do it??
help me please…
hey…i’ve tried…but i create my second cube, to my button on GUI…a tried so but not work…
var cube : GameObject;
var exist : int ;
function OnGUI(){
if (GUI.Button(Rect(10,10,100,50),"CreaCubo")){
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = Vector3(0, 1, 3);
cube.AddComponent("Rigidbody");
cube.rigidbody.AddForce(100,100,0);
exist = 1;
}
}
function OnCollisionEnter(){
if (exist)
Destroy(cube);
}
You need to make a separate script with your “OnCollisionEnter” function, and place that on the cube when you spawn it.
Place this script on an empty game object, call it something like “GUI”.
E.G.
var cube : GameObject;
var exist : int ;
function OnGUI(){
if (GUI.Button(Rect(10,10,100,50),"CreaCubo")){
cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
cube.transform.position = Vector3(0, 1, 3);
cube.AddComponent("Rigidbody");
cube.rigidbody.AddForce(100,100,0);
cube.collider.isTrigger = false;
cube.AddComponent(DestroyCube);
exist = 1;
}
}
Then place this function in another script called “DestroyCube”.
function OnCollisionEnter(collisioninfo : Collision){
Destroy(gameObject);
}
This will give you a button on screen, then when you press it, a cube will spawn. That cube should then be Destroyed once it collides with something. Also make sure that both objects that are hitting each other have colliders.