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 nathanvj · Jul 17, 2013 at 06:18 PM · screenfade

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. 


Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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);
     }
 }
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 nathanvj · Jul 19, 2013 at 05:59 PM 0
Share

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

avatar image robertbu · Jul 19, 2013 at 06:33 PM 0
Share

The 'player' tag goes on the player. Is your player a rigidbody with a collider or are you using a character controller?

avatar image nathanvj · Jul 27, 2013 at 05:52 PM 0
Share

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?

avatar image robertbu · Jul 27, 2013 at 06:36 PM 0
Share

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:

  1. The trigger is not firing

  2. The trigger is firing, but the name it is getting is not the one expected.

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

avatar image nathanvj · Jul 30, 2013 at 05:50 PM 0
Share

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

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

16 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Change screen resolution button in pause menu? 0 Answers

Fade In/Out Audio After Enter Trigger 3 Answers

Make damage screen ui image 1 Answer

x0,y0 doesn't always point to the bottom left corner 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