- Home /
Fade script working but I can't 'see' it working? (Solved)
Sorry for the cryptic title, but its actually quite accurate.
Searching the forums I've found this script that should fade-in and fade-out once my scene starts. I'm using unity5 which auto corrected the scripts and I'm getting no compiler errors.
If I play the game in window mode I can watch the inspector and things appear to be happening as they should - I can see the 'faded' variable changing from false - true. I can even see the alpha scale of the GUITexture decreasing and then increasing.
However on the screen (in-game) there is no visual representation of this. I mean nada.. no blackness, no colour change... nothing.
I see all the appropriate Debug and printed messages, so I'm assuming the script is working correctly?
using UnityEngine;
using System.Collections;
public class CollideAndFade : 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.
public bool Faded;
private bool fadeNow;
void Awake()
{
// Set the texture so that it is the the size of the screen and covers it.
GetComponent<GUITexture>().pixelInset = new Rect(0f, 0f, Screen.width, Screen.height);
Faded = false;
fadeNow = false;
}
void Update()
{
if(sceneStarting == true) // If the scene is starting...
{
StartScene(); // ... call the StartScene function.
}
if(fadeNow)
{
EndScene();
}
}
void FadeToClear()
{
// Lerp the colour of the texture between itself and transparent.
GetComponent<GUITexture>().color = Color.Lerp(GetComponent<GUITexture>().color, Color.clear, fadeSpeed * Time.deltaTime);
print ("I'm running!");
}
void FadeToBlack()
{
// Lerp the colour of the texture between itself and black.
GetComponent<GUITexture>().color = Color.Lerp(GetComponent<GUITexture>().color, Color.black, fadeSpeed * Time.deltaTime);
}
void StartScene()
{
// Fade the texture to clear.
FadeToClear();
// If the texture is almost clear...
if(GetComponent<GUITexture>().color.a <= 0.05f && sceneStarting)
{
// ... set the colour to clear and disable the GUITexture.
GetComponent<GUITexture>().color = Color.clear;
GetComponent<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.
GetComponent<GUITexture>().enabled = true;
// Start fading towards black.
FadeToBlack();
// If the screen is almost black...
if(GetComponent<GUITexture>().color.a >= 0.95f)
// ... reload the level.
Application.LoadLevel(2);
}
}
}
I'm thinking its something to do with my GUITexture (maybe pixel inset) I've not used this before so its a good chance that's where I'm being a noob.
I've tried a few variations for the X Y W H fields but nothing changed. The texture I'm using is a simple 32x32 black square.(Texture type : Texture)
What am I doing wrong please guys ???
Have you set up a debugger and stepped through execution to see what's going on? I made a tutorial for setting up VS and Unity 5 for free, if you don't like the $$anonymous$$ono debugger (if you're on Windows, you don't).
The program already has debug.logs in it and they seem to be working where/when they should
That's not nearly as effective :) There's too much code here for me to look at whilst at work. If you still don't have a solution when I'm home I'll take a second gander, but I really recommend getting setup with a step-debugger.
Try it. It will work :)
private float t = 0.0f;
public float fadeRate = 0.05f;
InvokeRepeating("fadeAway", 0.5f, fadeRate);
void fadeAway(){
t += fadeRate;
renderer.material.color = Color.Lerp(start, end, t / 2);
if (renderer.material.color.a <= 0.0)
CancelInvoke("fadeAway");
}
Woah I just noticed you're using GUITexture
. Switch to the new UI! Use a Sprite
ins$$anonymous$$d.
Answer by Bunny83 · Mar 19, 2015 at 05:29 PM
I don't see an immediate problem with the code (though it's a bit messy and uses too many GetComponent calls without caching ^^).
I would suggest you simply use my AutoFade script which you just need to drop into your project and whenever you want to change to another scene you can use
AutoFade.LoadLevel(levelName ,fadeOutDuration, fadeInDuration, color);
// or
AutoFade.LoadLevel(levelIndex ,fadeOutDuration, fadeInDuration, color);
Example:
AutoFade.LoadLevel("level2", 1.5f, 1.5f, Color.black);
This will do the same as Application.LoadLevel, but before it loads the new scene it fades to the specified color and when the new scene is loaded it fades out again.
ps: You don't need to attach the AutoFade script manually to anything. It will create an instance if needed.
Brilliant !
A little bit of tweeking to set it to fade when I need and it's perfect.
$$anonymous$$gestion - you may want to change/add some comments to that thread. I'm using unity5 which accepted the code 'as is' without having to make the changes that are needed to make it work (with 4.6)
When I made the changes initially it threw compiler errors, once I reverted back to the original version all ran fine.
Great script and thanks for the link.
:)