• 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
-1
Question by Detinator10 · Aug 14, 2014 at 03:53 AM · scenealphafading

How to fade in and out of a scene?

I want the scene to fade in from black to the actual scene. I have this code down here:

 using UnityEngine;
 using System.Collections;
 
 
 public class FadeScript : MonoBehaviour
 {
     public static Texture2D Fade;
     public bool fadingOut = false;
     public float alphaFadeValue = 1;
     public float fadeSpeed = 2;
     public Texture2D blackscreen;
     
     // Use this for initialization
     void Start ()
     {
     }
     
     // Update is called once per frame
     void OnGUI ()
     {
         alphaFadeValue -= Mathf.Clamp01(Time.deltaTime / 5);
         
         GUI.color = new Color(0, 0, 0, alphaFadeValue);
         GUI.DrawTexture( new Rect(0, 0, Screen.width, Screen.height ), blackscreen);
     }
     
 }

blackscreen is a completely black image by the way.

Anyway, the code doesn't work the screen starts out white (not black) and after a second it immediately turns to the scene. What's wrong here? Thank you!

Comment
Add comment · Show 1
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 smallbit · Aug 14, 2014 at 04:11 AM 0
Share

Have in $$anonymous$$d that OnGUI is called at least 2 times per frame, hence you shouldn't use delta time there, you should use update ins$$anonymous$$d.

Anyway this code works fine for me, it fades in around 1 second from black to none. So cannot help since i cannot reproduce the problem.

2 Replies

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

Answer by Detinator10 · Aug 14, 2014 at 06:25 PM

So it turned out that my orignal code did work. The white screen was just my level loading and unfortunately it was covering up my fade animation.

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
avatar image
1

Answer by rutter · Aug 14, 2014 at 04:36 AM

I think the issue is with the way you're keeping track of time. This works fine, for example:

 public class Fade : MonoBehaviour {
     Texture2D tex;
 
     public Color texColor = Color.black;
 
     public Color startColor = new Color(0f, 0f, 0f, 0f);
     public Color endColor = new Color(0f, 0f, 0f, 1f);
 
     public float duration = 2f;
 
     bool show;
 
     float timer;
 
     void Start() {
         //generate texture from scratch (optional)
         const int size = 512;
         tex = new Texture2D (size, size);
         for (int i=0; i<size; i++) {
             for (int j=0; j<size; j++) {
                 tex.SetPixel(i, j, texColor);
             }
         }
         tex.Apply();

         //call function in 3 seconds    
         Invoke("StartFade", 3f);
     }
 
     void StartFade() {
         timer = 0f;
         show = true;
     }
 
     void Update() {
         timer += Time.deltaTime;
     }
 
     void OnGUI() {
         if (show) {
             float percent = Mathf.Clamp01 (timer / duration);
             GUI.color = Color.Lerp (startColor, endColor, percent);
             GUI.DrawTexture (new Rect (0, 0, Screen.width, Screen.height), tex);
         }
     
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 Detinator10 · Aug 14, 2014 at 05:09 PM 0
Share

It doesn't work for me. The screen just starts white, fades for a second and then becomes black

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Unity documentation says to use StartCoroutine() method for visible fading of renderer alpha but the following method works fine without StartCoroutine() with visible intermediate values. Is this way also right ? 1 Answer

Color.a won't be shown/set correctly for values between 1 an 244 1 Answer

Screen fading while changing scenes doesn't seem to work. Help ! 1 Answer

Transparent/Diffuse-shader always transparent by default (alpha)? 1 Answer

Alpha mask to have a fading effect 2 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