Quantcast
Viewing latest article 4
Browse Latest Browse All 70

Code executed twice

Hey guys, I need some help tracking a bug in my script. What I'm trying to do is to Instantiate an object in the scene after a delay of 3 seconds. The code works as it is, but sometimes the code executes twice, so I get 2sets of particles and 2 objects instantiated( they don't run at the same time, but rather one after the other). I have a feeling that the solution is to use yield, but I do not know how to put that into practice. function Update() { if(resultedMix != null) { if(checkOnce) { resultedMix = null; if(resultedMix == null) { Destroy(Instantiate(GameObject.Find("LightGlow"), transform.position + Vector3(0, .5, 0), Quaternion(0,0,0,0)), 3.0); // Instantiate particles checkOnce = false; Invoke("SpawnObject", 3); } } } } function SpawnObject() { if(checkOnce == false) { clonedObject = Instantiate(GameObject.Find(resultedMix).transform, transform.position, Quaternion(0,0,0,0)); clonedObject.name = clonedObject.name.Substring(0, clonedObject.name.length - 7); clonedObject.transform.position = transform.position + Vector3(0, .4, 0); clonedObject.GetComponent(script_objectData).enabled = true; clonedObject.GetComponent(script_objectData).originalPosition = transform.position; clonedObject.tag = "mixed_lvl1"; GameObject.Find("cauldronLeftSlot").SendMessage("moveObject"); GameObject.Find("cauldronRightSlot").SendMessage("moveObject"); resultedMix = null; // clear the resultedMix var checkOnce = true; } } Thanks in advance!

Viewing latest article 4
Browse Latest Browse All 70

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>