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 LEMONVirus99 · Mar 17, 2013 at 07:57 PM · guijava

Fading GUI text

Ive been looking around and from different posts I've put together a script that has a GUI text in the middle of the screen for certain amount of time. However it doesn't seem to work properly. The GUI text is there but its very small and its not in one line, also it fails to fade away. could someone find where I've gone wrong. Thanks var color : Color;

 function Start()
 {
     color = Color.white;
     yield FadeOutAfterTime(5);
 }
  
 function OnGUI () {
     var centeredStyle = GUI.skin.GetStyle("Label");
     centeredStyle.alignment = TextAnchor.UpperCenter;
     GUI.Label (Rect (Screen.width/2-50, Screen.height/2-25, 100, 50), "Level 1", centeredStyle);
 }
  
 function FadeOutAfterTime(time : float)
 {
     yield WaitForSeconds(5);
     yield Fade();
 }
  
 function Fade()
 {
    while (color.a > 0)
    { 
        color.a -= Time.deltaTime;
        yield;
    }
 }
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

2 Replies

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

Answer by Griffo · Mar 17, 2013 at 08:42 PM

Add a GUI Text to your scene, make a new script, put this in -

 #pragma strict
 
 var color : Color;
 
 function Start(){
     
     color = Color.white;
 }
 
 function Update(){
 
     Fade();
 }
  
 function Fade(){
 
     while (guiText.material.color.a > 0){
         guiText.material.color.a -= 0.1 * Time.deltaTime * 0.05;
     yield;
     }
 }

In the inspector set to what you want, like below, using another font like I have to get a better and bigger effect -

alt text


grab-01.jpg (39.5 kB)
Comment
Add comment · Show 16 · 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 Griffo · Mar 17, 2013 at 08:45 PM 0
Share

Thats just the basis of the fade effect for the GUI Text, call and use it as you wish like in you question.

avatar image LEMONVirus99 · Mar 17, 2013 at 08:48 PM 0
Share

Just what i was after. Thank you!!

avatar image Griffo · Mar 17, 2013 at 09:33 PM 1
Share

Hope that helps you out, I've got to leave my computer now for some time so I wont be able to help for sometime but I'm sure someone here will help you out ..

avatar image Griffo · Mar 18, 2013 at 06:43 PM 1
Share

Sorry just noticed I put guiText and you cant use that, I've edited the script, should be O$$anonymous$$ now.

avatar image Griffo · Mar 18, 2013 at 08:02 PM 1
Share

Right lets start again ..

Create an empty game object, name it whatever you want, add a box collider and resize to around 5.5.5 and tick is Triggered in the inspector, then add this script -

     #pragma strict
 
     private var someText : GameObject;
 
     function Awake(){
 
         someText = GameObject.Find("GUItext");
         someText.gameObject.SetActive(false);
     }
 
     function OnTriggerEnter (other : Collider) {
 
         if(other.gameObject.tag == "Player"){
             someText.gameObject.SetActive(true);
             yield WaitForSeconds(10.0);  // Wait for X seconds then turn off the gameObject
             someText.gameObject.SetActive(false);
         }
     }

Now add GUI Text to the scene, rename it "GUItext" and add this script -

 #pragma strict
 
 var color : Color;
 
 function Start(){
     
     color = Color.white;
     Fade();
 }
 
 
 function Fade(){
     
     while (guiText.material.color.a > 0){
         guiText.material.color.a -= 0.1 * Time.deltaTime * 2;
         yield;
     }
     Destroy (gameObject);
 }

In the inspector set the GUItext as the picture I posted above,make sure whatever collides with the box collider in the first script above has the tag name of Player or change it in the script to whatever it's tag is, I've tested this and it works so good luck in following these instructions.

Show more comments
avatar image
1

Answer by michaelb212 · Jul 13, 2015 at 06:32 AM

Update this answer to unity 5:

Since color is a struct, the line:

 material.color.a -= 0.1 * Time.deltaTime * 2;

will be a compiler error.

The right way to do it would be saving the color to a local variable, or better using Color.operator-

 material.color -= new Color(0, 0, 0, 0.1 * Time.deltaTime * 2);

But the best way in my opinion is using Color.Lerp

 public float FadeoutTime;
 private Color startColor;
 private Color endColor;
 
 void Start()
 {
     startColor = GetComponent<Text>().color;
     endColor = startColor - new Color(0, 0, 0, 1.0f);
 }
     
 void Update () 
 {
     GetComponent<Text>().color = Color.Lerp(startColor, endColor, FadeoutTime)
 }

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

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

15 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

Related Questions

How To Make GUI Buttons Load/Quit 1 Answer

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

How do I create a loading screen and GUI system? 2 Answers

How to connect two functions (Javascript) 1 Answer

Setting Scroll View Width GUILayout 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