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 bromley · Feb 11, 2016 at 02:12 PM · screenlerpguitexturefadeoutfadein

Screen Fader Problem

I have a problem with a script that should run the Fade In and Fade Out between the start and the end of the scene. The script is this:

 using UnityEngine;
 using System.Collections;
 
 public class Fade_Screen_Scene : MonoBehaviour
 {
    public float fadeSpeed = 5.0F;   
    public GUITexture blackScreen;
    private bool sceneStarting = true;  
 
    void Awake()
    {
        blackScreen.pixelInset = new Rect (0f, 0f, Screen.width, Screen.height);
    }
 
 
    void Update()
    {
        if (sceneStarting == true)
        { 
        StartScene();
        }
    }
 
 
    void FadeToClear()
    {
        blackScreen.color = Color.Lerp(blackScreen.color, Color.clear, fadeSpeed * Time.deltaTime);
    }
 
 
    void FadeToBlack()
    {
        blackScreen.color = Color.Lerp(blackScreen.color, Color.black, fadeSpeed * Time.deltaTime);
    }
 
 
    void StartScene()
    {
        FadeToClear();
        if (blackScreen.color.a == 0.0f)
        {
            blackScreen.color = Color.clear;
            blackScreen.enabled = false;
            sceneStarting = false;
        }
    }
 
 
    public void EndScene()
    {
        blackScreen.enabled = true;
        FadeToBlack();
    }
 }

This script is put in a game object with a GUI Texture component

When the scene starts, a custom texture (in this case a black screen) Fade In, from black to clear. But when I press a trigger that enables the end of the scene, and calls the public void EndScene (), the texture DON'T Fade Out. I don't understand why and I tried so much. Please help me.

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 Landern · Feb 11, 2016 at 02:22 PM

It's because the EndScene gets called once, does a single step to fade and stops. When you're doing your StartScene method you support the continous calls each frame in the Update method that will show each step during a frame because of the method in the Update. Once faded(alpha set to 0) you're setting sceneStarting to false which will stop the StartScene method from being called in the Update Method.

You need to do something similar for your fade out so it can be called each frame until alpha is 1 or whatever you determine.

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 bromley · Feb 12, 2016 at 10:40 AM 1
Share

Thanks so much! I didn't see this important detail and now I fixed it. Here the new script:

     public float fadeSpeed = 5.0F;
     public GUITexture blackScreen;
     public float delayEnding = 2.0F;
 
     private bool sceneStarting = true;
     private bool sceneEnding = false;
     private IEnumerator coroutine;
 
     void Awake()
     {
         blackScreen.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
     }
 
 
     void OnGUI()
     {
         if (sceneStarting == true && sceneEnding == false)
         {
             StartScene();
         }
         else
         {
             EndScene();
         }
     }
 
 
     void FadeToClear()
     {
         blackScreen.color = Color.Lerp(blackScreen.color, Color.clear, fadeSpeed * Time.deltaTime);
     }
 
 
     void FadeToBlack()
     {
         blackScreen.color = Color.Lerp(blackScreen.color, Color.black, fadeSpeed * Time.deltaTime);
     }
 
 
     void StartScene()
     {
         FadeToClear();
         if (blackScreen.color.a == 0.0f)
         {
             blackScreen.color = Color.clear;
             blackScreen.enabled = false;
             sceneStarting = false;
         }
     }
 
 
     void EndScene()
     {
         blackScreen.enabled = true;
         FadeToBlack();
         if (blackScreen.color.a == 1.0f)
         {
             blackScreen.color = Color.black;
             blackScreen.enabled = false;
         }
     }
 
     public void CallEndScene()
     {
         coroutine = waitForEndScene(delayEnding);
         StartCoroutine(coroutine);
     }
 
     public IEnumerator waitForEndScene(float delayEnding)
     {
         yield return new WaitForSeconds(delayEnding);
         sceneEnding = true;
         StopCoroutine(coroutine);
     }
 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Animation Not Playing 0 Answers

How to fade in and out two panels in a scene? (c#) 1 Answer

How to fade in and out a image in a loop?,How do I fade in and out image on a loop? 0 Answers

Audio Fade In / Fade Out with Trigger 0 Answers

How do I use Mechanim to animate UI Image fade in and fade out? 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