- Home /
FadeOut when enter Collider errors.
I want to make the screen fade out when my car hits a invisible wall. This is my script, but I get errors...
#pragma strict
private var theCollider : String;
private var alpha = 1.0;
private var fadeDir = -1;
var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;
function OnTriggerEnter (other : Collider)
{
theCollider = other.tag;
if (theCollider == 'Player')
{
// FadeInOut
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);
}
//remember to set up the 2d texture / fadespeed / drawDepth.
Answer by robertbu · Jul 18, 2013 at 01:49 AM
You cannot nest function calls like this (OnGUI() inside of OnTriggerEnter()). You need to do something like:
#pragma strict
private var theCollider : String;
private var alpha = 1.0;
private var fadeDir = -1;
var fadeTexture : Texture2D;
var fadeSpeed = 0.2;
var drawDepth = -1000;
private var fade = false;
function OnTriggerEnter (other : Collider)
{
theCollider = other.tag;
if (theCollider == 'Player')
{
fade = true;
}
}
function OnGUI(){
if (fade)
{
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 do have to put this script on the invisible wall, with a player tag on it, don't I? If so it still doesn't work. Thanks anyways..
The 'player' tag goes on the player. Is your player a rigidbody with a collider or are you using a character controller?
I have an intro, a car is riding on a street, and if it's at the end of the street the screen needs to fade out. At the end of the screen I put the box collider, In the car are 2 people. I put the 'Player' tag to the car, and the script to the collider. It isn't working.. Any solutions?
First, figure out if your trigger is working. Either the trigger or the player need to be a Rigidbody before the code will work. Just above line 15, insert:
Debug.Log("Triggered: "+other.name);
Also insert on line 26:
Debug.Log("I'm fading...");
You are trying which of the following is causing your problem:
The trigger is not firing
The trigger is firing, but the name it is getting is not the one expected.
The fade is firing, but for some reason is not working.
Note I tested the script above in a very simple scene before I posted it.
Whenever I put a Rigidbody on the trigger it kind of flys away. I don't know how.. Same happens to the player/car. I don't know if this is common but I don't know how to fix it. So I can't try if the debug.log works. Thanks for your time. -