- Home /
Question by
Caiuse · Jul 23, 2011 at 04:30 PM ·
instantiatecoroutineontriggerenter
Problem OnTriggerEnter is calling more than once
function Update () {
if(Input.GetMouseButtonDown(0)){
StartCoroutine(blink(0.1));
}
}
function blink(time : float){
var originalPos = transform.position;
var targetPos = originalPos + Vector3(2.0, 0, 0);
var originalTime = time;
while (time > 0.0f){
time -= Time.deltaTime;
transform.position = Vector3.Lerp(targetPos, originalPos, time / originalTime);
yield;
}
}
function OnTriggerEnter(collision : Collider){
pos = Vector3(collision.transform.position.x + 0.1,transform.position.y,transform.position.z);
Instantiate(portal,pos,transform.rotation);
}
For some reason the instantiate is being called hundreds of times, so much so it crashes Unity. Any ideas - Caius
Comment
Answer by aldonaletto · Jul 23, 2011 at 05:10 PM
Despite coroutines and yield are always the first suspects in such cases, they seem ok. I suspect the problem is the trigger: you're instantiating the portal object at trigger's x position + 0.1, and z and y are the same as the owner of this script. If the portal object is a trigger, it will fire OnTriggerEnter again, which will generate another portal, which will fire OnTriggerEnter again, and so on.
Your answer