- Home /
Converting LoadLevelFade to C#
I'm trying to convert the LoadLevelFade script from the FPS Tutorial in order to understand how it works. I don't know JavaScript that well, but I do know C#.
I've converted most of the script to C#, but some of the lines are giving me errors or are ultimately confusing to me at the moment. One of them includes a supposed call to another function:
fade.GetComponent("Test").DoFade(level, fadeLength, false);
Unfortunately, the actual function call is bringing up errors. I've tried removing the GetComponent() call from the script and making the function in question static, but it creates other errors in that function as most of the variables used are also not static. However, making some of the variables static will ultimately result in greater headaches in the future.
I'm also having trouble wrapping my head around some of the yield calls included in the original JavaScript. I don't know why they're included in there to begin with.
Both the original and current version of the converted script are included in an attachment in a zip folder for others to look at.
Answer by Julien-Lynge · Jan 08, 2013 at 06:40 PM
There are many, many posts on converting between C# and JS. I've posted some links in a blog post for reference (http://fragileearthstudios.com/2011/10/18/unity-converting-between-c-and-javascript-2/) - since you're a bit more advanced, try the second one. Alternatively, just do a search in the top right widget on this site.
Answer by Rxanadu · Jan 09, 2013 at 02:19 AM
I'm still having trouble with the yields and coroutines in my script. I've tried following the examples in the documentation, but they only work for creating timers (i.e. with WaitForSeconds() calls).
Here's the area where I'm having the most trouble:
//fade texture in here
float time = 0.0f;
while (time < fadeLength) {
time += Time.deltaTime;
textureColor.a = Mathf.InverseLerp(0.0f, fadeLength, time);
StartCoroutine(DoFade(level, fadeLength, destroyTexture));
}
textureColor.a = 1;
guiTexture.color = textureColor;
yield return StartCoroutine(DoFade(level, fadeLength, destroyTexture));
Currently, the StartCoroutine() calls are causing the fade GameObject to be created indefinitely, causing an infinite loop and making it unable to stop the game.
If I try to remove the calls or replace them with null return yields, the script breaks and I get an error. If I start the coroutine in my Start enumerator, I get an error saying the coroutine can't start.
Overall, I'm still stumped even after using the links you provided, and I haven't seen any tutorials explaining how to use yields or coroutines in C# properly without using WaitForSeconds() calls (which I'm still not sure if that's the only way to do that process).
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Problem Javascript to C# 3 Answers
dealing with gameObject formations 1 Answer