c# - Coroutine stops working -
i have 2 scripts coroutine in them. works fine in first one, not in second one, no apparent reason.
it works in one:
using system.collections; using unityengine; using unityengine.ui; using unitystandardassets.imageeffects; public class gamestartcontroller : monobehaviour { public button startbutton; public gameobject cubespawner; // use initialization private void start() { startbutton = startbutton.getcomponent<button>(); } public void startgame() { enablecubespawner(); spawnstartingcubes(); hidestartmenu(); startcoroutine("focuscamera"); playbackgroundmusic(); } // enables cube spawner, can start spawning cubes private void enablecubespawner() { cubespawner.setactive(true); } private void spawnstartingcubes() { cubespawner.getcomponent<cubespawner>().generatestartingcubes(); } private void playbackgroundmusic() { var audio = gameobject.findwithtag("audio").getcomponent<audiocontroller>(); audio.playbackgroundmusic(); } private void hidestartmenu() { startbutton.transform.parent.getcomponent<canvasgroup>().interactable = false; startbutton.transform.parent.getcomponent<canvasgroup>().alpha = 0f; } private ienumerator focuscamera() { var camera = gameobject.findwithtag("maincamera").getcomponent<camera>(); var velocity = 0f; while (mathf.abs(camera.getcomponent<depthoffield>().aperture) > 0.001f) { debug.log(mathf.abs(camera.getcomponent<depthoffield>().aperture)); camera.getcomponent<depthoffield>().aperture = mathf.smoothdamp(camera.getcomponent<depthoffield>().aperture, 0f, ref velocity, 0.3f); yield return null; } camera.getcomponent<depthoffield>().aperture = 0f; } } the coroutine works fine , camera aperture goes smoothly 0.6 0.
however in second script doesn't happen:
using system.collections; using system.linq; using unityengine; using unitystandardassets.imageeffects; public class gameovercontroller : monobehaviour { public void endgame() { startcoroutine("unfocuscamera"); disablecubespawner(); destroyallcubes(); stopbackgroundmusic(); showstartmenu(); } // disables cube spawner, can stop spawning cubes private void disablecubespawner() { var cubespawner = gameobject.findwithtag("cubespawner"); cubespawner.setactive(false); } private void destroyallcubes() { var gameobjects = findobjectsoftype(typeof(gameobject)); foreach (var gameobject in gameobjects.where(gameobject => gameobject.name.contains("cube"))) { destroy(gameobject); } } private void stopbackgroundmusic() { var audio = gameobject.findwithtag("audio").getcomponent<audiocontroller>(); audio.stopbackgroundmusic(); } private void showstartmenu() { var startmenu = gameobject.findwithtag("startmenu"); startmenu.getcomponent<canvasgroup>().interactable = true; startmenu.getcomponent<canvasgroup>().alpha = 1f; } private ienumerator unfocuscamera() { var camera = gameobject.findwithtag("maincamera").getcomponent<camera>(); var velocity = 0f; while (camera.getcomponent<depthoffield>().aperture < 0.6f) { debug.log(mathf.abs(camera.getcomponent<depthoffield>().aperture)); camera.getcomponent<depthoffield>().aperture = mathf.smoothdamp(camera.getcomponent<depthoffield>().aperture, 0.6f, ref velocity, 0.3f); yield return null; } // camera.getcomponent<depthoffield>().aperture = 0f; } } it works 1 frame (aperture goes 0 0.03), , stops. why happening?
if destroy (or disable) game object, coroutines running on components attached stop. i'm unable find primary source on this, here 2 other stack overflow questions problem:
the coroutine stops because gameobject gameovercontroller attached destroyed. presumably unity checks whether object still exists before resuming coroutine, , if object destroyed, unity not continue execute it.
to fix problem, can delay destroying gameobject until animation complete (perhaps putting destroy code after while loop in coroutine) or put component on gameobject won't destroyed.
Comments
Post a Comment