Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 NIOS · Jul 15, 2015 at 08:47 PM · fadeout

How to make a scene fade out AFTER A FEW SECONDS?

So here's what I'm trying to do. I have a basic scene acting as a cutscene, and after twentyish seconds, it finishes and loads a new scene; the level. The contents are identical, except that the cutscene one, the camera cannot be moved by the player. Pretty simple premise. This is a nice way to make it so that my non-existent coding skills don't get in the way of messing up the scene.

I was trying to make it look neater... and I made the level scene fade in successfully. Likewise, I want to make the cutscene scene fade out when the time is up. I found a nice code that fades out perfectly, but it does so immediately. Here it is;

  #pragma strict
  
  // FadeInOut
  
  var fadeTexture : Texture2D;
  var fadeSpeed = 0.2;
  var drawDepth = -1000;
  
  private var alpha = 0.0; 
  private var fadeDir = -1;
  
  function OnGUI(){
  
      alpha -= fadeDir * fadeSpeed * Time.deltaTime;  
      alpha = Mathf.Clamp01(alpha);   
  
      GUI.color.a = alpha;
  
      GUI.depth = drawDepth;
  
      GUI.DrawTexture(Rect(0, 0, Screen.width, Screen.height), fadeTexture);
  }

I want the script to wait for about three or four seconds BEFORE fading.

I can't put in a yeild WaitForSeconds because its a coroutine, ....or something like that.

So I need to make the script called at a later time, or activated by something else, or something. I don't know enough about coding to know what to do. A little help? Thanks.

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 KdRWaylander · Jul 16, 2015 at 11:57 AM

You need to wrap the content of your OnGUI() function inside an if statement with a boolean. As long as the boolean is false, OnGUI does nothing, when it turns to true, OnGUI starts the fading process.

If you don't wanna use Coroutines, it's doable with the Update function:

You need a couple more variables:

 var fadeBool : boolean = false;
 var timer : float = 0;
 var secsToWait : float = 5; //number of seconds you want to wait before fading

You need an Update function that will increment your timer until you reach the number of seconds you want to wait before fade and then allow the fading:

 function Update () {
    if (timer < secsToWait){
       timer += Time.deltaTime; //adds the time between two frames to the timer
    } else {
       fadeBool = true;
    }
 }

Then you have to modify your OnGUI function:

 function OnGUI () {
    if (fadeBool) {
       // ... unmodified content
    }
 }



Comment
Add comment · Show 2 · 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 NIOS · Jul 16, 2015 at 10:59 PM 0
Share

Perfect. Worked wonderfully. Thank you so much for your help! Cheers!

avatar image KdRWaylander · Jul 17, 2015 at 07:10 AM 0
Share

You're welcome :)

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

2 People are following this question.

avatar image avatar image

Related Questions

AudioClip.Create fade in and out 1 Answer

fade from outside of your screen to the center 0 Answers

AudioSource FadeOut,Audio FadeOut 0 Answers

FadeIn and FadeOut Itween 0 Answers

Fadeout Issue 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