Unity: C# Function Survival Guide

Let’s say you have a function that fades the screen to black, loads a new level and then fades back in.

public IEnumerator LoadLevel(levelName) {

yield return StartCoroutine(FadeOut());
yield return StartCoroutine(FadeIn());

Said function lives in a script on an object that doesn’t get destroyed when a new level is loaded. But if you call that function from another script, the screen will fade out, the level will load, but the screen will never fade back in.


Well, if the caller gets destroyed the function will stop running and never reach the point where it could fade back in.

And the easy solution is: Create a second function (in the same script as LoadLevel of course) that does nothing but start the primary function.

public void InitiateLoadLevel(string levelName) {
Bookmark the permalink. Follow any comments here with the RSS feed for this post. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>