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
1
Question by Digital-Phantom · Mar 19, 2015 at 03:20 PM · textureguitextureonguifadepixelinset

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.

alt text

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

guitexture.png (21.8 kB)
Comment
Add comment · Show 9
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 tigertrussell · Mar 19, 2015 at 03:30 PM 0
Share

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

avatar image Digital-Phantom · Mar 19, 2015 at 03:50 PM 0
Share

The program already has debug.logs in it and they seem to be working where/when they should

avatar image tigertrussell · Mar 19, 2015 at 03:57 PM 0
Share

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.

avatar image saud_ahmed020 · Mar 19, 2015 at 04:08 PM 0
Share

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");
     
     }
 

  




avatar image tigertrussell · Mar 19, 2015 at 04:15 PM 0
Share

Woah I just noticed you're using GUITexture. Switch to the new UI! Use a Sprite ins$$anonymous$$d.

Show more comments

1 Reply

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

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.

Comment
Add comment · Show 1 · 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 Digital-Phantom · Mar 19, 2015 at 06:35 PM 0
Share

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.

:)

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

24 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

GUI.DrawTexture on GUI.Button press 1 Answer

GUI on iPhone...GUI.DrawTexture or guiTexture.pixelInset ? 1 Answer

Another Inventory Script with pictograms 0 Answers

GUI.Box not showing Texture on device 1 Answer

Draw a waveform on a mobile texture 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