Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 laurienash · Oct 04, 2013 at 02:18 PM · collisionfade

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

Comment
Add comment · Show 3
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 vexe · Oct 04, 2013 at 02:26 PM 1
Share

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...

avatar image laurienash · Oct 04, 2013 at 03:03 PM 0
Share

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)

avatar image laurienash · Oct 04, 2013 at 03:04 PM 0
Share

I tried doing the opposite, by tagging the colliders- but this had the same issue.

1 Reply

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

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.

Comment
Add comment · Show 7 · 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 laurienash · Oct 04, 2013 at 04:20 PM 0
Share

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;
 
 }
 }
avatar image Xtro · Oct 04, 2013 at 04:42 PM 1
Share

you renamed the Update method to isBlack(). So there is no update event. Change it back to Update() man :) :)

avatar image laurienash · Oct 04, 2013 at 05:10 PM 0
Share

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?

avatar image Xtro · Oct 04, 2013 at 05:17 PM 1
Share

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);
avatar image laurienash · Oct 04, 2013 at 05:43 PM 0
Share

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;
  }
 }
Show more comments

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

17 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

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


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