gameobject destroyed on collision with character

hey guys , so i have a problem with my script , i have a character and a box ( i want the box to be destroyed when the character jump on the box) , the problem is , my script works only if the box hits the character , but me i want the box to be destroyed when the character hits the box not the opposite , here’s my script ! thanks

function OnCollisionEnter(collision:Collision){
     if(collision.gameObject.tag == "Player"){
          Destroy(gameObject);
     }
}

Since the player is a CharacterController, you should use OnControllerColliderHit instead - but there’s a problem: this event is only sent to the character script. A simple and efficient solution is to tag the boxes with some specific tag (“box”, for instance), and place the code in the character script:

function OnControllerColliderHit(hit: ControllerColliderHit){
  if (hit.transform.CompareTag("box")){
    Destroy(hit.gameObject);
  }
}

By just saying Destroy(gameObject) you’re referencing the gameObject that the script is attached to - so if it’s the box, it’ll destroy the box. You can either specify the target of the collision to be destroyed

Destroy(collision.gameObject);

Or you can put the script onto the player, and make a new tag called like KillBox or something, then do:

function OnCollisionEnter(collision:Collision){
     if(collision.gameObject.tag == "KillBox"){
          Destroy(gameObject);
     }
}

That should work. I suspect it’d be a lot easier to do it the first way though :wink:

This looks interestingly like something I am trying to accomplish, EXCEPT, I want to make an Object Visible after I, the Player, Collides with it. Don’t want to kill it. My PROBLEM is that I can not see where to plug in the Transform.Position and Transform.Rotation.

My code is:

using UnityEngine;
using System.Collections;

public class Invisibility : MonoBehaviour {
	public Rigidbody SteppingStone9;
	
   	void OnTriggerEnter(Collider col){
	 if (col.gameObject.tag == "Player"){
		Rigidbody Step = Instantiate(SteppingStone9, transform.position, transform.rotation) as Rigidbody;
	}	
		
}
}

IF we REPLACED the Instantiate with the isVisible type control, it would work.
I would not mind if the code was JS, vice, C#.
I am not too good on JS, yet.

I am thinking something like this in JS …

var isVisible : boolean;

function Start(){
//Step starts out Invisible
isVisible = false;

            }

function Update ()
{

   function OnCollisionEnter(collision:Collision){
 if(collision.gameObject.tag == "Player"){
            isVisible = true;
            collider.isTrigger = true;

                                          }
                                                  }
 }