Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Story Specialist · Oct 06, 2011 at 10:35 AM · c#coroutinemathf.clamp

Mathf.Clamp01 CoRoutine

I'm trying to fade individual pieces of the GUI in C#. Just simple phrases that pop up on the screen and then fade out over time, like small tutorials to my games controls. For this, I took code from a ScreenFade script. My (shortened) code is as follows:

     void Awake () {
         StartCoroutine(TutorialAddPhrase(3));
         c = Color.white;
         cDelta = Color.white;
         alpha = 1.0f; 
         fadeSpeed = 1.0f;
         fadeDir = -1;
     }
 
     void OnGUI () {
 
             print (cDelta.a);
             GUI.color = cDelta;
             cDelta.a = alpha;
             GUI.Label (new Rect (200, 200, 200, 100), "TESTING");
     }
 
     void TutorialAddPhrase (int wait) {
             
             yield return new WaitForSeconds(wait);
             alpha += fadeDir * fadeSpeed * Time.deltaTime; 
             alpha = Mathf.Clamp01(alpha);            
     }

This renders the following error: Assets/Scripts/GUIscript.cs(197,14): error CS1624: The body of GUIscript.TutorialAddPhrase(int)' cannot be an iterator block because void' is not an iterator interface type

If I change that last function from "void" to "IEnumerator", it doesn't give an error, but it doesn't really properly fade out the alpha. The alpha stops at 0.9863821 (for instance, it differs).

What am I doing wrong here? I hope you can point me in the right direction. I've only just begun working in C#, so it confuses me at times.

Comment
Add comment
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

1 Reply

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

Answer by syclamoth · Oct 06, 2011 at 11:35 AM

The problem you are having here, is that StartCoroutine only executes once! That means, that after the WaitForSeconds happens, the bits afterwards don't repeat. Things using Time.deltaTime need to go into the Update or FixedUpdate loops, otherwise they aren't much use to you.

What you probably intended to do, was wait 3 seconds, and then fade the alpha. In order to do that, you need to set up a boolean

 bool fading = false;

then in your coroutine, set it to true.

 IEnumerator TutorialAddPhrase (int wait) {

        yield return new WaitForSeconds(wait);
        fading = true;
 }

then to do the actual fading, have something in Update:

 void Update()
 {
     if(fading)
     {
         alpha = Mathf.Clamp01(alpha + (fadeDir * fadeSpeed * Time.deltaTime));
     }
 }

Another problem is that in your GUI function, you are treating GUI.Color as a reference type, when it is in fact a value type. This means you can't assign GUI.color to some color, and then expect it to change if you change the original- it only assigns what that color is at the exact moment you use it.

Instead, do this-

 Color tempColor = new Color(cDelta.r, cDelta.g, cDelta.b, alpha);
 GUI.color = tempColor;
Comment
Add comment · Show 18 · 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 Story Specialist · Oct 09, 2011 at 02:55 PM 0
Share

It works, thank you very much! And I think I understand what you're saying about the reference/value type, as well. $$anonymous$$akes sense.

One more thing you might want to adjust to your code: the if statement just says "(fading)", ins$$anonymous$$d of "(fading == true)". Thanks again! :D

avatar image syclamoth · Oct 09, 2011 at 04:07 PM 0
Share

yeah,

 if(fading)

is EXACTLY THE SA$$anonymous$$E as

 if(fading == true)

just so you know. If statements always use bool values, and anything else you put in there has to be able to convert into a boolean before the if can handle them- however, actual booleans can just be passed straight in!

avatar image Eric5h5 · Oct 09, 2011 at 04:54 PM 0
Share

I would strongly recommend not using Update, and ins$$anonymous$$d just make the fading happen in the coroutine. That avoids all those awkward "if (fading)" constructions.

avatar image Eric5h5 · Oct 11, 2011 at 02:12 PM 2
Share

@syclamoth: http://www.unifycommunity.com/wiki/index.php?title=Fade A fade doesn't change every frame, it only changes for a given time and then stops. Update is called every frame forever.

avatar image Story Specialist · Oct 12, 2011 at 10:16 AM 1
Share

@ Eric5h5 : So I've read over your linked article multiple times, but I can't get it to work. In C, I have the following code: StartCoroutine (Fade.use.Alpha(thisObject, 0.0f, 1.0f, 5.0f, EaseType.In)); (thisObject is an assigned variable)

And I get the following error: Object reference not set to an instance of an object

StartCoroutine (Fade.use.Alpha(tut_Add, 0.0f, 1.0f, 5.0f, EaseType.In)); (with tut_Add being the GUIText) doesn't work either. What am I doing wrong?

(I don't know why Unity Answers is parsing http:// urls, I of course don't have those in the actual code.)

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

c# Using an IEnumerator yield WaitForSeconds to temporarily pause a While loop 3 Answers

WWW, RESTful Service, and Threading 1 Answer

Coroutine does not stop when having a coroutine inside. 2 Answers

Var change between 2 RPC calls / Coroutine problem 1 Answer

Coroutines and Static function 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