OnCollisionStay

Hello, I would like to instantiate an object when a value is set to True. I have been fighting with this code and cannot seem to get it right. I would appreciate some help. I have two scripts: script1 supposed to set up the value to true if collision happen and script2 create the instantiation. Right now the value passed to script2 is always false therefore no instantiation is happening. Thanks.

static var hasPos1Contact = false;

function Update () {

	if(hasPos1Contact==true)
	{
		hasPos1Contact=true;
	}
	else
{
	hasPos1Contact = false;
}
}

function OnCollisionStay(collision : Collision) {

for (var contact : ContactPoint in collision.contacts) {

	hasPos1Contact = true;
}

}
function Update()  {

if(other.hasPos1Contact==true)
{
Instantiate (button, Vector3(85.89714, -74.61391, 0),Quaternion.identity);
}
static var hasPos1Contact = false;

function Update () {
	if(hasPos1Contact==true)
	{
		hasPos1Contact=true;
	}
	else
{
	hasPos1Contact = false;
}
}

function OnCollisionEnter(collision : Collision) {
for (var contact : ContactPoint in collision.contacts) 
	Instantiate (button, contact.point,Quaternion.identity);//Vector3(85.89714, -74.61391, 0)

}

Swap the OnCollisionStay for OnCollisionEnter, and move the instantiate over to the onCollisionEnter part. That way, it only instantiates when you hit something, not when you are in contact with something. Though cool to have that swap over to a different script, it is not necessary in this case.

Thanks BigMister for your reply. It’s working now. I was using OnCollisionStay thinking that when the object is no longer in contact the instantiation will disapear.