Box Destroy Drop

Hi all.I have a script that almost works.
I want my box on collision to be destroyed, and to add a prefab.It does all that but i can’t make it destroy on collision.O and i wanted it to be a rigidbody box, so it doesnt fall trough platform.So like when my ball jumps on it it crshes it.If someone can help Thank you .Here’s the script.

// The items in the box.
// You assign these items to valid prefabs in the editor.
var items = new GameObject[1];

// Set this value to true to cause the box to be destroyed
// and the items created.
var boxDestroyed = false;

// Update is called once per frame
function LateUpdate () 
{
    if (boxDestroyed)
    {
        var position = transform.position;
        // Clone the objects that are "in" the box.
        for (item in items)
        {
            if (item != null)
            {
                // Add code here to change the position slightly
                // so the items are scattered a little bit.
                Instantiate(item, position, Quaternion.identity);
            }
        }
        // Get rid of the box.
        Destroy(gameObject);
    }
}

function OnTriggerEnter ( other : Collider) {

    

if (other.gameObject.tag == "Player"){

	var boxDestroyed = true;

}

}

you write in ontriggerenter: var boxdestroyed = true what means you declare a new variable with the same name which covers your intended one and is destroyed at the end of the function .so your intended boxdestroyed is never set to true but a temporary variable is. leave the var in front of it away and it should work as intended.

Hi.I didn’t know it was that simple.
Thank you. :slight_smile:

One more question is there a way i can add, when i hit the box whit certain amount of force it crashes(Destroys).Ty :slight_smile:

Bump!

Hmm, get velocity and mass to generate a semi-solid formula? Not quite sure how one might approach that…

Thatbadguy is right, using rigidbody.velocity and mass (velocity is Vector3, .magnitude is float value) you can calculate the impulse value using a pretty basic formula. Here’s a wiki link: Impulse (physics) - Wikipedia

Can someone help me i made an script, but i get and error at OnColisionEnter.Here’s the script
As i saide i wanted my script on certain force to destroy the box.

// You assign these items to valid prefabs in the editor.
var items = new GameObject[2];

// Set this value to true to cause the box to be destroyed
// and the items created.
var boxDestroyed = false;

// Update is called once per frame
function LateUpdate () 
{
    if (boxDestroyed)
    {
        var position = transform.position;
        // Clone the objects that are "in" the box.
        for (item in items)
        {
            if (item != null)
            {
                // Add code here to change the position slightly
                // so the items are scattered a little bit.
                Instantiate(item, position, Quaternion.identity);
            }
        }
        // Get rid of the box.
        Destroy(gameObject);
    }
}

function OnCollisionEnter ( collision : Collision) {

if (collision.relativeVelocity.ma*gnitude > 5  collision.gameObject.tag == "Player")
{
boxDestroyed = true;
}


}

The error is like this:

Thank you…

What the, I can’t write that thing with hashtag in my comment. Is that thing in the script too?

I need someone to help me fix the script i gave.
Whitout that error i quoted showing up or if someone can help me somehow .Thank you.

Must… Learn… How… to … DEBUG!!!

var items = new GameObject[];
var explosionRadius : float = 3.0;

function OnCollisionEnter ( collision : Collision) {
      if (collision.relativeVelocity.magnitude > 5  collision.gameObject.tag == "Player"){
            var position = transform.position;
            // Clone the objects that are "in" the box.
            for (item in items){
                  if (item != null){
                      // Add code here to change the position slightly
                      // so the items are scattered a little bit.
                      var pos = position + Random.insideUnitSphere * explosionRadius;
                      Instantiate(item, pos, Random.rotation);
                  }
              }
              // Get rid of the box.
              Destroy(gameObject);
      }
}

Rewrite with the proper way to do what you are asking. (setting a variable to happen on the next frame is not the best way to do it, if it happens now… do it now.)

Hi.BigMisterB thank,thank,thank you.This problem has been Fixed thanx to you.