Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by CB-TV · Nov 11, 2015 at 09:59 AM · functionyieldwaitforsecondsrepeatinggenerating

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!";
     }
 }

Comment
Add comment · Show 6
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Bonfire-Boy · Nov 11, 2015 at 10:23 AM 0
Share

Have you tried "Explicitly declare the type of either one.." as it suggests ?

avatar image CB-TV Bonfire-Boy · Nov 11, 2015 at 10:28 AM 0
Share

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.

avatar image Bonfire-Boy CB-TV · Nov 11, 2015 at 10:47 AM 0
Share

I was assu$$anonymous$$g that it meant specifying the return types of the functions. This kind of thing...

 function FadeOut() : IEnumerator
 {
 }
avatar image Bonfire-Boy · Nov 11, 2015 at 10:50 AM 0
Share

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.

avatar image CB-TV Bonfire-Boy · Nov 11, 2015 at 02:25 PM 0
Share

Before I added in the repeating portion, the text faded in, then out fine. That's what I meant.

avatar image Runalotski · Nov 11, 2015 at 12:22 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CB-TV · Nov 11, 2015 at 08:29 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

35 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges