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 KampKounslr · Dec 02, 2015 at 07:10 PM · coroutinesdeltatime

Weird issue with l_ElapsedTime value only incrementing properly one time

Hello,

I am making a splash screen scene for my game and am trying to alter the alpha value of an overlaying black image to give the effect of fading in and out the splash image. I have gotten it to fade in with no problems, however when I am trying to fade it back out my l_ElapsedTime is not incrementing by Time.deltatime, instead the elapsed time value bounces around to different amounts... usually (but not always) below .333. I tried incrementing by 0.5f instead of Time.deltatime, and ran into a similar problem (the value usually remaining at 0.5).

I have tried a number of things but have run into the same problem each time. I tried having a completely separate function that performs the fade out effect, using different values for elapsed time for fade in and fade out, not using the += operand (instead typing it entirely out), using StopCoroutine, and more. Not sure where to go with this now. Oddly I am also unable to get the splash screen sound to play as well.

Code inserted below, I appreciate any help you can offer.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 
 public class FadeLogo : MonoBehaviour {
 
     // **Global Variables**
 
     [SerializeField]
     private float g_FadeTime = 2.5f;            // The amount of time it takes to fade the image in or out.  
 
     [SerializeField]
     private AudioSource g_SoundEffects;         // The audio source for the splash screen sound, which will be played back on the Sound Effects mixer output. 
 
     [SerializeField]
     private AudioClip g_SplashSound;            // The audio clip which will be played. 
 
     [Header("UI")]
     [SerializeField]
     private CanvasGroup g_FadeCanvas;           // The canvas with image to fade in and out. 
 
     private bool g_FadeIn = true;
     private bool g_FadeOut = false; 
 
 
     // **Functions**
     
     // Update is called once per frame
     void Update () {
         StartCoroutine (SplashLoop());
     }
 
     private IEnumerator SplashLoop()
     {
         yield return StartCoroutine (FadeScreen(g_FadeIn));
         StopCoroutine("FadeScreen");
         yield return StartCoroutine (DelayBeforeFadeOut());
         yield return StartCoroutine (FadeScreen(g_FadeOut));
         g_FadeCanvas.alpha = 1.0f;              // This here to ensure the image stays faded out. 
     }
 
     // This function will fade in or out the splash screen image by adjusting the FadeCanvas' alpha value. 
     private IEnumerator FadeScreen (bool FadeIn)
     {
         float l_ElapsedTime = 0.0f;
 
         while (l_ElapsedTime < g_FadeTime)
         {
             if (FadeIn)         // Fade in the splash logo
             {
                 g_FadeCanvas.alpha = 1.0f - (l_ElapsedTime / g_FadeTime);    
             }
             else                // It's not set to fade in, so fade out the splash logo
             {
                 g_FadeCanvas.alpha = (l_ElapsedTime / g_FadeTime);
             }
 
             l_ElapsedTime += Time.deltaTime;
 
             Debug.Log(l_ElapsedTime + "Fade: " + FadeIn);
             yield return null;
         }
     }
 
     // This function delays the processing of the FadeScreen function to fade out splash screen, allows the user to see the splash screen unfaded and hear the splash screen sound. 
     private IEnumerator DelayBeforeFadeOut()
     {
         g_SoundEffects.clip = g_SplashSound;
         Debug.Log("Sound ready to play: " + g_SoundEffects.clip.loadState);
         g_SoundEffects.Play();
 
         yield return new WaitForSeconds(g_SoundEffects.clip.length + 1.0f);
     }
 }

 
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
0
Best Answer

Answer by KampKounslr · Dec 02, 2015 at 07:19 PM

Figured it out. Had the splash loop being called in Update instead of Awake. Blargh.

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

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

34 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

Related Questions

Doubts with rotation, deltatime and Clamp 0 Answers

Best way to run a long algorithm in the background? 1 Answer

Strange Problems in long scripts 0 Answers

Yield return statement ends loop execution 0 Answers

"Can't add script behaviour AICharacterControl. The script needs to derive from MonoBehaviour!" ? 0 Answers


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