- Home /
Resources.load loads twice ( onTriggerenter->load-another->destroy-self )
Scenario : OnTriggerEnter, a new object should be loaded/instantiated from Resources and after loading/instaniating new object, colliding/triggering object should be destroyed.
Script :
function OnTriggerEnter(collider : Collider) {
Instantiate(Resources.Load("object", GameObject), Vector3(Random.Range(-3,3),2,0),Quaternion.identity);
Destroy(gameObject);
}
Problem : Sometime object gets loaded/instanced twice. I am not sure what is causing the object to be instanced twice.
What else I have tried but did not work :
I have also tried disabling the collider as soon as it enters the trigger event. I have tried yield Wait and separating the instructions in a separate coroutine. I have tried OnCollisionEnter instead of OnTriggerEnter.
Thanks
Answer by SkaredCreations · Mar 22, 2014 at 06:41 PM
May be it's spawning the new object inside the collider? Since you're destroying the collider object, try to disable the collider before using Instantiate:
function OnTriggerEnter(otherCollider : Collider) {
collider.enabled = false;
Instantiate(Resources.Load("object", GameObject), Vector3(Random.Range(-3,3),2,0),Quaternion.identity);
Destroy(gameObject);
}
Thanks for your input. Unfortunately, it didn't work