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 /
This question was closed Jul 09, 2014 at 05:31 PM by Blayer98 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Blayer98 · Apr 05, 2014 at 04:11 PM · functionloopguitextfaderepeat

How do I repeat a part of my script?

I want to repeat a part of my code:

 function Start(){ 
     guiText.material.color.a = 0;
     yield WaitForSeconds(3);
         FadeIn();
         }  
 function FadeIn(){
     while (guiText.material.color.a < 1){
        guiText.material.color.a += 0.1 * Time.deltaTime * 10;
        yield;    
        }  
        yield WaitForSeconds(0.1);
        FadeOut();  
        }
  
 function FadeOut(){
     while (guiText.material.color.a > 0){
        guiText.material.color.a -= 0.1 * Time.deltaTime * 10;
        yield;    
        }
 }    

I want to repeat the "FadeIn" and "FadeOut" parts, how will I be able to do that? I tired StartCoroutine, but it was just to difficult...

Comment
Add comment · Show 1
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 Blayer98 · Apr 05, 2014 at 09:14 PM 0
Share

Thanks! It works! :3

2 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Ermarrero · Apr 05, 2014 at 04:59 PM

Just make a

     function Update() 
     { 
       FadeIn(); 
       FadeOut(); 
     }

Replace the while loop to a if statement. Also remove FadeOut () from the FadeIn (), thats no longer needed.

Have you thought about putting them both in one function instead of two?

Another thing you can do and its reallly simple is using Mathf.PingPong. See below:

Heres a reallllly simple solution:

 public class TextFader : MonoBehaviour
 {
     public GUIText text;
     public float speed = 1f;
     public Color color;
 
     void Start () 
     {
         if(text == null)text = this.gameObject.GetComponent<GUIText>();
         if(text == null)Debug.Log("Please attach this script to a gameobject with a GUIText component or assign one manually to this.");
 
     }
 
     void Update ()
     {
         if (text != null)
         {
             text.color = color;
             color.a = Mathf.PingPong(Time.time / speed, 1);
         }
     }
 }
Comment
Add comment · Show 2 · 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 Ermarrero · Apr 05, 2014 at 05:39 PM 0
Share

https://docs.unity3d.com/Documentation/ScriptReference/$$anonymous$$athf.PingPong.html

Look at this mathf class espcially the pingpong. It will do what you need on one line of code.

avatar image Ermarrero · Apr 06, 2014 at 12:36 AM 0
Share

Heres a reallllly simple solution:

 public class TextFader : $$anonymous$$onoBehaviour
 {
     public GUIText text;
     public float speed = 1f;
     public Color color;
 
     void Start () 
     {
         if(text == null)text = this.gameObject.GetComponent<GUIText>();
         if(text == null)Debug.Log("Please attach this script to a gameobject with a GUIText component or assign one manually to this.");
         
     }
     
     void Update ()
     {
         if (text != null)
         {
             text.color = color;
             color.a = $$anonymous$$athf.PingPong(Time.time / speed, 1);
         }
     }
 }
avatar image
0

Answer by 0V3RR1D3 · Apr 06, 2014 at 01:04 AM

Or

 function FadeIn(){
     while (guiText.material.color.a < 1){
        guiText.material.color.a += 0.1 * Time.deltaTime * 10;
        yield;    
        }  
        yield WaitForSeconds(0.1);
        FadeOut();  
        }
  
 function FadeOut(){
     while (guiText.material.color.a > 0){
        guiText.material.color.a -= 0.1 * Time.deltaTime * 10;
        yield;    
        }else{
        FadeIn();
       
     }
 }
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

Follow this Question

Answers Answers and Comments

23 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

Related Questions

Trying to Loop A Function 2 Answers

Fade Float Function 2 Answers

Repeat a function only when an action occurs 1 Answer

Looping a function 1 Answer

Calling functions in a specific order. 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