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 alilograpi · Mar 28, 2013 at 10:18 PM · disappearslowly

Need help with GUI text !

So I've got a script that change the GUI.text after a time period and I want to make it slowly disappear before the next one appear and not just change but I have no idea how to do that , can anyone help me ?

there is my script :

 using UnityEngine;
 using System.Collections;
  
 public class untrotext : MonoBehaviour {
  
 void Start () {
 StartCoroutine(showStuff());
 }
  
 // A predone set of messages over time
 IEnumerator showStuff() {
 transform.guiText.text="Firing...";
 yield return new WaitForSeconds(2);
 transform.guiText.text="Missile is fired";
 // put some text in second line:
 transform.Find("lineTwo").guiText.text="Woe to them";
 yield return new WaitForSeconds(2);
 transform.guiText.text="";
 transform.Find("lineTwo").guiText.text="";
 }
  
 // someone else can call this for any 1-time message:
 public IEnumerator flashMessage(string msg, float len) {
 transform.guiText.text=msg;
 yield return new WaitForSeconds(len);
 transform.Find("lineTwo").guiText.text="l";
 }
 }

Thank you for your time.

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
0
Best Answer

Answer by Lockstep · Mar 28, 2013 at 10:26 PM

The best way to create a fade effect, is to manipulate the alpha channel of the color of the thing you want to fade. In case of a guitext you need to make sure there is a material attached. The font material can easily be found as a child of the font in the project panel. For the scripting part: You need a function that changes the alpha value of the color over time. Something like this:

 public float fadeTime = 0.5f; //How long do we fade
 
 IEnumerator fadeText (bool fadeIn){
     float startTime = Time.time;
     Color c = guitext.material.color;
     Color c_invis = new Color(c.r, c.g, c.b , 0f);
     Color c_vis = new Color(c.r, c.g, c.b , 1f);
     if(fadeIn){
         while(Time.time - startTime < fadeTime){   //Loop for fadeTime seconds
              guitext.material.color = Color.Lerp(c_invis, c_vis, (Time.time - startTime) / fadeTime);
              yield return WaitForEndOfFrame();
         }
     } else {
         while(Time.time - startTime < fadeTime){   //Loop for fadeTime seconds
              guitext.material.color = Color.Lerp(c_vis, c_invis, (Time.time - startTime) / fadeTime);
              yield return WaitForEndOfFrame();
         }
     }
 
 }
Comment
Add comment · Show 4 · 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 alilograpi · Mar 29, 2013 at 03:36 PM 0
Share

Thanks the script is really useful but how do I combine those two script ? when I try to use the fade in function on my first script I get an error that it cant be used in this context ...

avatar image fafase · Mar 29, 2013 at 03:41 PM 0
Share

It is a IEnumerator method, so it called using

  StartCoroutine(fadeText(true));

$$anonymous$$ake sure the StartCoroutine is in a piece of code that does not get called each frame:

 if(fadeIn){
    StartCoroutine(fadeText(true));
    fadeIn = false;
 }
avatar image Lockstep · Mar 29, 2013 at 04:09 PM 0
Share

What fafase is saying is right. You must call it with StartCoroutine and have only one instance of it running at a time. If you need more flexibility, then you could introduce a variable which keeps track of the alpha value. $$anonymous$$aybe like this:

 pulic float fadeTime = 0.5f;
 public bool fadeIn = false;
 private float alphaCounter = 0f;
 private Color c_invis = new Color(c.r, c.g, c.b , 0f);
 private Color c_vis = new Color(c.r, c.g, c.b , 1f);
 
 void Start(){
     Color c = guitext.material.color;
     c_invis = new Color(c.r, c.g, c.b , 0f);
     c_vis = new Color(c.r, c.g, c.b , 1f);
 }
 void Update(){
     ChangeAlpha();
 }
 void ChangeAlpha(){
     if(fadeIn) alphaCounter += Time.deltaTime / fadeTime;
     else alphaCounter -= Time.deltaTime / fadeTime;
     alphaCounter = $$anonymous$$athf.Clamp01(alphaCounter);
     guitext.material.color = Color.Lerp(c_invis, c_vis, alphaCounter); 
 
 }
avatar image alilograpi · Mar 30, 2013 at 11:51 AM 0
Share

Ok , thanks a lot =)

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

12 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

Related Questions

Multiple Cars not working 1 Answer

Multiple Weapon Animation Help 0 Answers

Object Disappears on Start 0 Answers

Light disappears by flashlight 1 Answer

UnityEngine.Input.GetMouseButton(1)) issue 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