Adding a variable to instantiated object causes infinite objects to spawn.

function OnCollisionEnter(collision : Collision)
{
	transform.position.x = 0;
	if (collision.relativeVelocity.magnitude > 1)
	{
		health -=15 * damagePower;
	}

	if(health < 0)
	{
		var temp:Quaternion = transform.rotation;
		var crate = Instantiate(blox, transform.position, temp);
		crate.transform.scale -= 0.5;
		Destroy (gameObject);
		
	}
}

Using this script I am instantiating a smaller box when the large one has it’s health go below 0. Works fine, makes one object, and doesn’t crash the system or anything when I remove “crate.transform.scale -= 0.5;”. In fact, if I remove the transform.scale and try to pass on

any variable to the object it seems to try creating an infinite number of objects.

why? also, how do I make it actually work?
thanks

edit:

I tried it with the new script you gave me. I edited out the collision point since what I need is the new block to spawn at the same location and angle as the one just destroyed. I also forgot to reattach my target in the engine to the blox:GameObject. Once I got it sorted out I getting the scale to work, but not passing any script defined variables. This is the script I am using on both the original block and the same block with the same script attached as the spawn:

var health : int = 16;
var blox : GameObject;
var blockType:int = 3;
function Start()
{
	if(blockType >0)
	{
		health = 100 * blockType;
	}
	else
	{
		health = 100;
	}
}
function OnCollisionEnter(collision : Collision) {

 transform.position.x = 0;
 if (collision.relativeVelocity.magnitude > 5)
 {
 	health -=100;	// for now just kill the block with one hit
 }

if (health <= 0){
    var rot : Quaternion = transform.rotation;
    var pos : Vector3 = transform.position;
    var crate = Instantiate(blox, pos, rot);
    crate.transform.localScale = Vector3(0.5,0.5,0.5);
    //change the new instantiated block to blockType 6 only using that as and example,
// it normally would be crate.blockType = blockType; to pass on
//the same blockType var this block used
    crate.blockType = 6;
    // Destroy the original crate
    Destroy (gameObject);
  }else{
   //Debug.Log("Nothing spawned");
  }
}

I have several blocks in the scene and each blockType of the original can be set in the engine, but the new block spawned is from the same prefab they all use so a large healthy block spawns the same blockType as a smaller weak block instead of passing on it’s block type used in engine. This is my current error in the console “MissingFieldException: Field ‘UnityEngine.GameObject.blockType’ not found.”
I am trying to use the same block prefab and the same script on the object for multiple block types so I need to pass on the blockType var.

ofset the blox, your spawning it inside the object, making it run through the collision again, offseting it should fix this. EG:

function OnCollisionEnter(collision : Collision)
{
 transform.position.x = 0;
 if (collision.relativeVelocity.magnitude > 1)
 {
 health -=15 * damagePower;
 }

 if(health < 0)
 {
 var temp:Quaternion = transform.rotation;
 var crate = Instantiate(blox, transform.position + Vector3(0,1.5,0), temp);
 crate.transform.scale -= 0.5; //needs to be crate.transform.localScale -= Vector3(0.5,0.5,0.5);
 Destroy (gameObject);
 
 }
}

var crate = Instantiate(blox, transform.position + Vector3(0,1.5,0), temp);

Now on the spawned object, add a script taking 1.5 away from the Y axis, but add a slight delay, make it yield WaitforFixedUpdate(); after the Start function.

You need to get the components, it wont treat it like its a varable built into the gameobject type. you need crate.getComponent(“ThisScript”).type = 6;, like the example below.

var health : int = 16;

var blox : GameObject;

var blockType:int = 3;

function Start()

{

 if(blockType >0)

 {

 health = 100 * blockType;

 }

 else

 {

 health = 100;

 }

}

function OnCollisionEnter(collision : Collision) {



 transform.position.x = 0;

 if (collision.relativeVelocity.magnitude > 5)

 {

  health -=100; // for now just kill the block with one hit

 }



if (health <= 0){

    var rot : Quaternion = transform.rotation;

    var pos : Vector3 = transform.position;

    var crate = Instantiate(blox, pos, rot);

    crate.transform.localScale = Vector3(0.5,0.5,0.5);

    //change the new instantiated block to blockType 6 only using that as and example,

// it normally would be crate.blockType = blockType; to pass on

//the same blockType var this block used

    crate.gameObject.GetComponent("ThisScriptName").blockType = 6;

    // Destroy the original crate

    Destroy (gameObject);

  }else{

   //Debug.Log("Nothing spawned");

  }

}