Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Dogg · Jul 20, 2014 at 01:03 AM · c#scenefade

Fading To New Scene Problem?

Hello, I'm trying to fade one scene to another, but I'm having trouble. The screen fades in into the first scene I have loaded, but it does not fade out into another scene. I don't know why this is happening, so maybe I'm not seeing something. Here's the script so you guys can see for yourselves:

 public float fadeSpeed = 1.5f;          // Speed that the screen fades to and from black.
     
     
     private bool sceneStarting = true;      // Whether or not the scene is still fading in.
 
     private bool Faded;
     
 
 
     void Awake ()
     {
         // Set the texture so that it is the the size of the screen and covers it.
         guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
         Faded = false;
     }
     
     
     void Update ()
     {
         // If the scene is starting...
         if(sceneStarting)
             // ... call the StartScene function.
             StartScene();
     }
     
     
     void FadeToClear ()
     {
         // Lerp the colour of the texture between itself and transparent.
         guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
     }
     
     
     void FadeToBlack ()
     {
         // Lerp the colour of the texture between itself and black.
         guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
     }
     
     
     void StartScene ()
     {
         // Fade the texture to clear.
         FadeToClear();
         
         // If the texture is almost clear...
         if(guiTexture.color.a <= 0.05f)
         {
             // ... set the colour to clear and disable the GUITexture.
             guiTexture.color = Color.clear;
             guiTexture.enabled = false;
             Faded = true;
             Debug.Log("True");
             
             // The scene is no longer starting.
             sceneStarting = false;
         }
     }
     
     
     void EndScene ()
     {
         if (Faded == true)
         {
         // Make sure the texture is enabled.
         guiTexture.enabled = true;
         
         // Start fading towards black.
         FadeToBlack();
         
         // If the screen is almost black...
         if(guiTexture.color.a >= 0.95f)
             // ... reload the level.
             Application.LoadLevel(2);
         }
    }
 }

So why is it not fading to a different scene? The part that's not working is the function EndScene. It's not setting the guiTexture to true, and is not loading a new scene. By the way this script was from a tutorial. I simply added and changed some things like the LoadLevel, the if statement, the Faded bool, and so on.

Comment
Add comment · Show 4
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 gjf · Jul 20, 2014 at 10:01 AM 0
Share

hate to state the obvious, but you're never calling EndScene() from anywhere...

avatar image Manco Capac · Jul 20, 2014 at 01:11 PM 0
Share

also u need this script attached to some game object in your level2. or else it will just fade to black and stay there.

avatar image Dogg · Jul 20, 2014 at 08:26 PM 0
Share

Okay so I made some adjustments to two scripts. The one I posted, and my $$anonymous$$ain$$anonymous$$enuScript, which is the scene I want to fade into after my first scene. I tried calling the script from the $$anonymous$$ain$$anonymous$$enuScript, and here's how it looks:

 public class $$anonymous$$ain$$anonymous$$enu : $$anonymous$$onoBehaviour {
 
     GameObject scriptName;
     
     void Start(){
                     scriptName.GetComponent<SceneFadeInOut> ().EndScene ();
             }
 

In my $$anonymous$$ain $$anonymous$$enu Scene, I have the SceneFadeInOut attached to a GUI Texture, just like my first scene. It fades in when I play it, just like the first scene. I also get this error: NullReferenceException UnityEngine.GameObject.GetComponent[SceneFadeInOut] () (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/UnityEngineGameObject.cs:28) $$anonymous$$ain$$anonymous$$enu.Start () (at Assets/Scripts/$$anonymous$$ainmenu/$$anonymous$$ain$$anonymous$$enu.cs:26)

So after all of this, the first scene is still not fading out and loading the $$anonymous$$ain $$anonymous$$enu Scene.

avatar image Dogg · Jul 21, 2014 at 01:29 AM 0
Share

Still looking for help, can anyone please help?

1 Reply

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

Answer by drex150 · Jul 21, 2014 at 06:26 AM

Your StartScene(); is inside the function Update. function Update runs every frame so StartScene(); is running every frame until sceneStarting becomes false.

On the other hand, EndScene(); is never being called, let alone not being called in the update function. So even if you are calling it elsewhere, if you're not calling it in the update, it is only going to run one time and then stop until you tell it to run again.

Basically, you need EndScene(); to be called in the update function as long as some kind of variable is true.

Here is a very simple version of your code that fades to clear and then automatically turns back around and fades to black.

Notice EndScene(); is in the update with a fadeNow variable. Just make fadeNow true whenever you want the screen to fade out.

 using UnityEngine;
 using System.Collections;
 
 public class test : MonoBehaviour {
 
     public float fadeSpeed = 1.5f;          // Speed that the screen fades to and from black.
     
     
     private bool sceneStarting = true;      // Whether or not the scene is still fading in.
     
     private bool Faded;
 
     private bool fadeNow;
     
     
     
     void Awake ()
     {
         // Set the texture so that it is the the size of the screen and covers it.
         guiTexture.pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
         Faded = false;
         fadeNow = false;
     }
     
     
     void Update ()
     {
         // If the scene is starting...
         if(sceneStarting)
             // ... call the StartScene function.
             StartScene();
         if(fadeNow)
         {
             EndScene();
         }
     }
     
     
     void FadeToClear ()
     {
         // Lerp the colour of the texture between itself and transparent.
         guiTexture.color = Color.Lerp(guiTexture.color, Color.clear, fadeSpeed * Time.deltaTime);
         print ("I'm running!");
     }
     
     
     void FadeToBlack ()
     {
         // Lerp the colour of the texture between itself and black.
         guiTexture.color = Color.Lerp(guiTexture.color, Color.black, fadeSpeed * Time.deltaTime);
         //print ("I'm running!");
     }
     
     
     void StartScene ()
     {
         // Fade the texture to clear.
         FadeToClear();
         
         // If the texture is almost clear...
         if(guiTexture.color.a <= 0.05f && sceneStarting)
         {
             // ... set the colour to clear and disable the GUITexture.
             guiTexture.color = Color.clear;
             guiTexture.enabled = false;
             Faded = true;
             Debug.Log("True");
             
             // The scene is no longer starting.
             sceneStarting = false;
         }
         if(!sceneStarting)
         {
             fadeNow = true;
         }
     }
     
     
     void EndScene ()
     {
         if (Faded == true)
         {
             // Make sure the texture is enabled.
             guiTexture.enabled = true;
             
             // Start fading towards black.
             FadeToBlack();
             
             // If the screen is almost black...
             if(guiTexture.color.a >= 0.95f)
                 // ... reload the level.
                 Application.LoadLevel(2);
         }
     }
 }
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 Dogg · Jul 21, 2014 at 08:58 PM 0
Share

Awesome thank you so much drex. Quick question, do you know how to stop it from continuously fading after it loads the Application.LoadLevel(2)? So like it fades in to the first scene, then fades out, then loads the next scene, and then it just keeps fading in over and over. So the screen on scene 2 becomes black over and over.

avatar image Dogg · Jul 21, 2014 at 11:13 PM 0
Share

Never $$anonymous$$d I figured it out. Thanks a lot again drex.

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

24 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

Related Questions

Multiple Cars not working 1 Answer

How to Set a bool to Individual Scenes (C#) 1 Answer

Third person movement similar to Max Payne 1 Answer

Scene changing 1 Answer

Make player not be seen by AI, when player in foilage and shadows. 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