Cannot Call Function from Another Function
What I want the code to do below is to fade in a piece of text, fade it out. Change the text, then repeat. I have a variable which gets added to each time the text is faded out so that when it fades in, it has changed. The fading in and out works fine, I haven't tried to see if changing the text works but the main problem is repeating.
I have a line of code at the bottom of the FadeOut function telling it to start the FadeIn function but it gives me an error:
"Definition of 'Controls.FadeOut()' depends on 'Controls.FadeIn()' whose type could not be resolved because of a cycle. Explicitly declare the type of either one to break the cycle."
This is because both functions sort of depend on each other and so the compiler cannot generate the code for both.
All I want is a to repeat a function when a different function ends, for the text to change when it ends as well, and for it to fade in and out smoothly. Could someone tell me how I could fix this problem? Thank you!
My script:
var nextText = 0;
function Start ()
{
GetComponent.<TextMesh>().color.a = 0;
yield WaitForSeconds (5);
FadeIn ();
}
function FadeIn ()
{
while (GetComponent.<TextMesh>().color.a < 1)
{
GetComponent.<TextMesh>().color.a += 1.5 * Time.deltaTime * 2;
yield;
}
yield WaitForSeconds (5);
FadeOut ();
}
function FadeOut ()
{
while (GetComponent.<TextMesh>().color.a > 0)
{
GetComponent.<TextMesh>().color.a -= 1.5 * Time.deltaTime * 2;
yield;
}
yield WaitForSeconds (5);
nextText ++;
FadeIn ();
}
function Update ()
{
if (nextText == 0)
{
GetComponent.<TextMesh>().text = "Right click to turn your torch on!";
}
if (nextText== 1)
{
GetComponent.<TextMesh>().text = "Press Spacebar to jump!";
}
if (nextText== 2)
{
GetComponent.<TextMesh>().text = "Press E to pick up objects!";
}
}
Have you tried "Explicitly declare the type of either one.." as it suggests ?
I tried to add a return at the end of them as a Unity Answers suggested but it didn't work as there is a yield in the function. I'm not sure if this is "Explicitly declaring" it but that's what I've tried anyway.
I was assu$$anonymous$$g that it meant specifying the return types of the functions. This kind of thing...
function FadeOut() : IEnumerator
{
}
By the way, can you explain your comment "The fading in and out works fine"? Seeing as the code doesn't compile, it's hard to understand how it can be working fine.
Before I added in the repeating portion, the text faded in, then out fine. That's what I meant.
Why not a fadInOut function that would use a sine wave so it reaches 1 and 0 over time then you can just turn it off and on easily.
Answer by capz-nst · Nov 11, 2015 at 01:26 PM
i'm not entirely sure about javascript but dont you need to start a coroutine to be able to use yield? other than that, it would probably be better to do all of the logic in a single function call.
for example: while() do fade in, yield waitforseconds while() do fade out, yield update the text
and put this in a loop that breaks when you've dealt with all of your text
I managed to make it work with your help. But I've come into another problem: I cannot change the nextText variable. With the script below, I have managed to add 1 to the variable each time it fades in through a separate function. The problem with this is that the text changes as the text is fading out (if I remember correctly). I've literally tried every way to make it work but I think I need to do it a different way. How should I initiate the nextText function and where? Should I do it this way or is there a better way to add 1 to nextText each time it fades out? Thanks!
#pragma strict
var nextText = 0;
var fadingIn = false;
function Start ()
{
GetComponent.<Text$$anonymous$$esh>().color.a = 0;
}
function Update ()
{
print (nextText);
FadeInOut ();
if (nextText == 0)
{
GetComponent.<Text$$anonymous$$esh>().text = "Right click to turn your torch on!";
}
if (nextText == 1)
{
GetComponent.<Text$$anonymous$$esh>().text = "Press Spacebar to jump!";
}
if (nextText == 2)
{
GetComponent.<Text$$anonymous$$esh>().text = "Press E to pick up objects!";
}
}
function FadeInOut ()
{
yield WaitForSeconds (5);
while (!fadingIn)
{
GetComponent.<Text$$anonymous$$esh>().color.a += 1.5 * Time.deltaTime * 2;
yield;
yield WaitForSeconds (5);
NextText ();
fadingIn = true;
yield WaitForSeconds (5);
}
while (fadingIn)
{
GetComponent.<Text$$anonymous$$esh>().color.a -= 1.5 * Time.deltaTime * 2;
yield;
yield WaitForSeconds (5);
fadingIn = false;
}
}
function NextText ()
{
if (!fadingIn && GetComponent.<Text$$anonymous$$esh>().color.a < 1)
{
nextText += 1;
}
}
Your answer
Follow this Question
Related Questions
Delay in setting Boolean Value 1 Answer
Unity WaitForSeconds Not Working Inside Update or a Loop 2 Answers
The Legend of the waiting for function to end! Still no answer for that, Please Help 1 Answer
How to add multiple yield return new wait for seconds inside an IEnumerator? 1 Answer
WaitForSeconds clarification 1 Answer