I'm Noob...please hel collision...

Hi!i’m new in unity,and i have one problem…

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… :cry:

tnx…

sorry for my english but i’m italian!

Attach a script to the one you want destroyed with the following code inside:

function OnCollisionEnter()
{
   Destroy(gameObject);
}

If you want to do anything besides just have it destroy itself, you’ll have to add more in, but that’ll handle the destruction just fine.

hey!i understand now…tank you! :slight_smile: :smile:

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);

}

how can i do it?
tnx…

guys…help…when i try the script…i haven’t error!!but the script don’t work when my cubes collider!!!whi!!

Make sure the cube collider is not a trigger. You’ve also left out the function parameters for OnCollisionEnter
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); 
var boxCollider = cube.AddComponent(BoxCollider);
boxCollider.isTrigger = false;
exist = 1; 

} 
} 

function OnCollisionEnter(collisionInfo : Collision){ 

if (exist) 
Destroy(cube); 

}

If the collider IS a trigger, use the “OnTriggerEnter” function. If it IS NOT a trigger, us “OnCollisionEnter”.

tank you!!!but is not a trigger…i try now your script! :slight_smile:

ok i do this…

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);
var boxCollider = cube.AddComponent(BoxCollider);
boxCollider.isTrigger = false;
exist = 1;

}
}


function OnCOllisionEnter(collisioninfo : Collision){

if (exist){

Destroy(cube);
}
}

i attached this script to main camera but when i play one error message compared…

NullReferenceException: Object reference not set to an instance of an object…

i think so say this because when i create the primitive cube, this have just a boxcollider attached…no?

my problem is to my primitive cube…because not collide with first cube(character)when touch it…

ok i do this…i attached this script on my first cube…

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;
exist = 1;

}
}


function OnCOllisionEnter(collisioninfo : Collision){

if (exist){

Destroy(cube);
}
}

now no error compared…but my primitive cube…not Destroy!! :evil: uff…

Sorry it’s really late here, I wasn’t thinking.

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.

tnx! yes i say that!ok i try… :slight_smile:

TnX!!work it!!but now if collide on my plane…it destroy…i say if collide with any other collider don’t destroy…right?