Collision Destroy Help?

I’m completely new to Unity, and I’m trying to make it so when the first person Object collides with a capsule, the capsule - and another separate one - will disappear from the game. As I said, I’m new to Unity, and don’t understand much of the coding every other answer displays, such as “function OnCollisionEnter ( ){ GameObject.Destroy ( gameObject); }” besides the obvious “Destroy”. A simple answer with a thorough explanation fit for a beginner would be very much appreciated.

objects have colliders attached.

colliders can be thought of a shape which has the special property that it notices which objects are touching or are inside the bounds of the shape.

all colliders on collision automatically as part of unity call the function

OnCollisionEnter()

that is attached to the game object the collider is attached to

so game objects which is anything. can have collider which is a component attached to it.

components basically are parts you bolt onto a game object that give it additional functionality.

A rigidbody for example allows a gameobject to behave according to the laws of physics. It has built into it all the complicated code that simulates physics. if you want an object to collide with another object you could create it add a rigidbody and go

rigidbody.addforce(0,10,0) and it would shooot upwards at 10 meters per second and as long as gravity was checked gravity would automatically be applied and if it ran into something with a collider it would automatically stop and not pass through and you dont need to code that physics interaction in where things bounce off properly or anything. The nividia physx engine is built into unity and all that code just magically occurs.

Components are in some sense wrappers. They are pretty little covers that hide lots of complex things and you just feed them stuff and you get back stuff and you don’t need to know or even want to know how they do what they do.

you don’t know how a computer monitor figures out what keys to shows when you press a keyboard, nor do you know it displays it.

you just want to be able to type debug.log(“blah”)
and have blah show up.

Likewise components add functionality. You know they do because they have functions. functions like addforce or oncollisionenter.

these functions allow you to basically throw tons of lines of code onto an object by just attaching this one component in a half second.

so now we know that a rigidbody is required for physics interactions like collisions.
thats because normally moving stuff like saying

transform.position = something

thats not a physics based movement. basically your teleporting stuff around.
using addforce() however does.
it has tons of lines of codes that makes sure force based movements check for collisions and bounce off stuff and basically obey the laws of physics.

so when an object with a rigidbody touches an object with a collider it automatically causes the object which was collided with to call the function OnCollisionEnter

that function is by default empty. so what your doing when you type that is actually redefining the function. but just for this object. That way each object can have its own special way to react to colliisons.

now there is also this thing called a parameter. the thing in parathesis after the name

OnCollisionEnter(Collider thecolldier)

the name of it is “thecollider” thats not important, its just a variable, you can call it whatever you want, what “thecollider” is is the actual collider attached to whatever hit you.

so you ahve a person and a bullet
the bullet has a rigidbody and force was added and it’s traveling along and it hits the gameobject person. Person has a collider attached so when the bullet hits it it automatically internally goes

Person.OnCollisionEnter(bullets collider)

so it calls the function OnCollisionEnter attached to person and runs whatever code is in that script

collider is attached to the bullet but you can backtrace to the actual gameobject for exmaple

bulletcollider.gameobject will give us the actual bullet gameobject and not just the collider.

so lets see something in practice. lets have the bullet and we’ll just say the bullet does damage and has a damage int stored in it and were going to lower the health of the player by whatever the damage of the bullet is

this script will be attached to the player

Void OnCollisionEnter(Collider WhateverHitMe){
 damage = WhateverHitMe.GameObject.getcomponent<bulletstats>().damage;
health = health - damage;
}

this may appear a little daunting

whateverhitme is well whatever hit you but in this case it’d be a bullet.
so thats a collider. now all colliders are attached to game obejcts. so what we do first is go

whateverhitme.gameobject
that gets us to the core object the gameobject. its the root of the treee

now we get a component attached to the gameobject. scripts are themselves considered components so any script attached to an object can be accessed by object.getcomponent

now we need to tell it what component to get and we say get the component and in <> we put the name of the attached script. so inside stats there will a public variable called damage.

so bullet has a script called bulletstats and it has a variable called damage that is whatever damage we want that bullet to do.

so we get the game object of the collider that hit us, get the script attached to that gameobject then get the damage variable value stored int aht script.

then we lower the value of health stored in player.