- Home /
Run a Prefab while ONLY in the trigger zone.
Hi guys!
Been puzzled by this for the last 4 hours. In a nutshell, I only want my Particle PreFab to run while my "Player" tag is in the trigger zone. Right now, I can instantiate the Prefab "Visual" when "Player" enters the trigger zone however its trying to stop the prefab when the "player" leaves that I'm finding difficult. My current code obviously gives me "Destroying assets is not permitted"
As I'm still a bit new i'm having trouble accessing the myPart Prefab (Clone) to delete upon my "Player"'s exit trigger.
Any help is much appreciated.
public GameObject visual; void OnTriggerEnter(Collider col) { if (col.gameObject.tag == "Player") { GameObject myPart = (GameObject)Instantiate (visual); } } void OnTriggerExit(Collider col){ if (col.gameObject.tag == "Player") { Destroy(visual); } }
Answer by DajBuzi · Feb 11, 2014 at 08:07 PM
Just store visualClone as an empty GameObject variable:
private GameObject visualsClone;
Then when entering the "zone" assign new instance to this variable:
void OnTriggerEnter(Collider col){
if(visualsClone == null) visualsClone = Instantiate(Visuals) as GameObject;
}
And when exiting zone:
void OnTriggerExit(Collider col){
if(visualsClone != null) Destroy(visualsClone.gameObject);
}
Thanks very much for that DajBuzi.
At first your code destroyed the player so I added "Destroy(visualsClone);" ins$$anonymous$$d of "Destroy(col.gameObject);"
and it worked great for what i needed. $$anonymous$$uch appreciated.
Yea i made mistake at first but it should be fine now.
I'm happy to hear that this helped you :)