- Home /
Script attached to collider being activated when player hits different collider
Hi - I've written a script which fades the scene black when my player hits a trigger.
I've attached it to a certain collider - and the first time I play the game it works fine. Every time after that however - the scene turns black when the player is set to active at a different collider to which this script is not attached - rather than when the player, after being set to active, hits a trigger later on, to which this script is attached.
Do you have any idea why this is?
var intensityBlack:float = 0;
private var IsBlack:boolean = false;
private var cLight:GameObject;
function Start() {
cLight = GameObject.Find("Lighting");
}
function OnTriggerEnter(other:Collider) {
if(other.tag == "Carl") {
IsBlack = true;
}
}
function Update() {
if(IsBlack) {
if(cLight.renderer.material.color.b != 0 ) {
cLight.renderer.material.color.a -=Time.deltaTime*intensityBlack;
if( cLight.renderer.material.color.a<=0)//set to black as the current fading is to white
cLight.renderer.material.color = Color(0,0,0,0);
}
else
cLight.renderer.material.color.a +=Time.deltaTime*intensityBlack;
}
}
Thanks in advance, Laurien
Well Unity doesn't deal with Voodoo (at least to my knowledge) - are you sure those colliders don't have your script?
what if you did the opposite? - check for collision in your player script, give your colliders a tag, like "FadeTrigger", and in your player:
function OnTriggerEnter(other : Collider)
{
if (other.tag == "FadeTrigger")
{
other.GetComponent<TriggerScript>().IsBlack = true;
}
}
See what happens...
And btw I don't see you setting IsBlack to false again anywhere...
Hi thanks - I think its the IsBlack that's the issue.
The first time I run through the game, it works as expected. But if earlier on in the game, it has faded to black (due to a different triggers) and has gone back to the title scene -- and I replay the game - then this script no longer works: and the game fades to black as soon as the new player is instantiated.
I don't know how to solve this though - do you have any ideas? (Sorry it's quite tricky to explain)
I tried doing the opposite, by tagging the colliders- but this had the same issue.
Answer by Xtro · Oct 04, 2013 at 04:11 PM
You should set the IsBlack to false in some point. Right?
if it stays as true, update will run it always.
Hi - so I changed it like this: but then the script doesn't run at all, am I doing it wrong?
#pragma strict
var intensityBlack:float = 0;
private var IsBlack:boolean = false;
private var cLight:GameObject;
function Start() {
cLight = GameObject.Find("Lighting");
IsBlack = false;
}
function OnTriggerEnter(other:Collider) {
if(other.tag == "Carl") {
IsBlack = true;
}
}
function isBlack() {
if(IsBlack) {
if(cLight.renderer.material.color.b != 0 ) {
cLight.renderer.material.color.a -=Time.deltaTime*intensityBlack;
if( cLight.renderer.material.color.a<=0)//set to black as the current fading is to white
cLight.renderer.material.color = Color(0,0,0,0);
}
else
cLight.renderer.material.color.a +=Time.deltaTime*intensityBlack;
}
}
function OnTriggerExit(other:Collider) {
if (other.tag == "Carl") {
IsBlack = false;
}
}
you renamed the Update method to isBlack(). So there is no update event. Change it back to Update() man :) :)
Uhoh how embarassing... I should probably get more sleep :/
Is there a replacement to OnTriggerExit do you know? So it is set to false once the screen has turned black, so that the player doesn't have to stay inside the collider for the screen to turn fully black?
setting it to false should be done just where you want it to be.
if you want it to be end of the fading to black screen, then do it after you complete fading to black.
maybe just after this line ???
cLight.renderer.material.color = Color(0,0,0,0);
Yes - I tried that already, but the problem is it only works the first time. (Each time the screen goes black the title scene loads and then on pressing the spacebar the game loads again) But the second time the game is played - the screen doesn't fade to black.
#pragma strict
var intensityBlack:float = 0;
private var IsBlack:boolean = false;
private var cLight:GameObject;
function Start() {
cLight = GameObject.Find("Lighting");
}
function OnTriggerEnter(other:Collider) {
if(other.tag == "Carl") {
IsBlack = true;
}
}
function Update() {
if(IsBlack) {
if(cLight.renderer.material.color.b != 0 ) {
cLight.renderer.material.color.a -=Time.deltaTime*intensityBlack;
if( cLight.renderer.material.color.a<=0)//set to black as the current fading is to white
cLight.renderer.material.color = Color(0,0,0,0);
IsBlack = false;
}
else
cLight.renderer.material.color.a +=Time.deltaTime*intensityBlack;
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Getting radius of a sphere 2 Answers
How to attract moving object to specific areas -1 Answers
Fade in and out, texture separation 1 Answer
Safe area from enemies 1 Answer