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 0mr_ashutosh0 · Nov 09, 2014 at 12:18 PM · randomupdatewaitforsecondsframerateinvokerepeating

How to slow down a function call in C# unity

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class Myclass: MonoBehaviour {
 
     RectTransform rt;
     Text txt;
     public float fadeSpeed;
     
     void Start () {
         txt = gameObject.GetComponent<Text>(); // Accessing the text component
     }
     
     void Update () {
         ColorChange();
     }
 
     void ColorChange()
     {
         txt.color = Color.Lerp (txt.color, txt.color, fadeSpeed * 5 * Time.deltaTime);
         int random = Random.Range(1,5);
 
         if(random==1)
             txt.color=Color.red;
         else if(random==2)
             txt.color=Color.cyan;
         else if(random==3)
             txt.color=Color.magenta;
         else if(random==4)
             txt.color=Color.green;
     }
 }
 


Here The update keeps calling ColorChange everyframe and my Lerp is useless here because of this.

I want to save framerate and all of unnecessary resources wasted by these calls Is there way i can optimize this by just calling my Colorchange every 3 or 5 seconds with Lerp working properly for lets say 3 seconds and changing the color slowly.

Please suggest some optimized way to save unnecessary calls to functions.

I had tried IEnumerator, waitforseconds, and then there is Invokerepeating But non of them seems to be working because of Update keep calling the changeColor.

Comment
Add comment · Show 5
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 richyrich · Nov 09, 2014 at 12:32 PM 0
Share

You could just check the time and if not enough has passed, don't execute the function

avatar image 0mr_ashutosh0 · Nov 09, 2014 at 12:36 PM 0
Share

so if i call changeColor from Update i have to check some fix time before i call it right ? that means update will keep running and keep checking time to meet the criteria ?

avatar image 0mr_ashutosh0 · Nov 09, 2014 at 12:52 PM 0
Share

I tried that still not working

avatar image OrbitGames · Nov 09, 2014 at 01:13 PM 0
Share

try InvokeRepeating

avatar image 0mr_ashutosh0 · Nov 09, 2014 at 01:15 PM 0
Share

I tried that but now my Lerp is not working.

public class $$anonymous$$yclass: $$anonymous$$onoBehaviour {

 Text txt;
 public float fadeSpeed=2.0f;
 
 void Start () {
     txt = gameObject.GetComponent<Text>(); // Accessing the text component
     InvokeRepeating("ColorChange",2 ,2 );
 }

 void ColorChange()
 {
     Debug.Log("Calling");
     txt.color = Color.Lerp (txt.color, txt.color, fadeSpeed * Time.deltaTime);


     int random = Random.Range(1,5);

     if(random==1)
         txt.color=Color.red;
     else if(random==2)
         txt.color=Color.cyan;
     else if(random==3)
         txt.color=Color.magenta;
     else if(random==4)
         txt.color=Color.green;

 }

}

$$anonymous$$y Lerp is quickly changing color no smooth transition

1 Reply

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

Answer by richyrich · Nov 09, 2014 at 02:50 PM

 public float fadeSpeed = 2f;
 Color oldColor = Color.red;
 Color newColor = Color.white;
 float fadeAmount = 0;
 
 // Update is called once per frame
 void Update ()
 {
     ColorChange();
 }

 void ColorChange()
 {
     Debug.Log("Calling");
     fadeAmount += fadeSpeed * Time.deltaTime;
     txt.color = Color.Lerp(oldColor, newColor, fadeAmount);

     if (fadeAmount>1f)
     {
         fadeAmount = 0;
         oldColor = txt.color;
         int random = Random.Range(1, 5);

         if (random == 1)
             newColor = Color.red;
         else if (random == 2)
             newColor = Color.cyan;
         else if (random == 3)
             newColor = Color.magenta;
         else if (random == 4)
             newColor = Color.green;

     }
 }
Comment
Add comment · Show 3 · 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 richyrich · Nov 10, 2014 at 01:01 PM 0
Share

If this solution has solved the problem, please mark as accept so that others don't need to check how to solve a solved problem. Otherwise, please indicate what else you need to satisfy.

avatar image 0mr_ashutosh0 · Nov 12, 2014 at 02:44 PM 0
Share

Hey @richyrich Thanks with some modifications the code worked perfectly. Here is the full code

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class $$anonymous$$yclass: $$anonymous$$onoBehaviour {
 
     Text txt;
     public float fadeSpeed = 2f;
     Color oldColor = Color.red;
     Color newColor = Color.white;
     float fadeAmount = 0;
 
     void Start () {
         txt = gameObject.GetComponent<Text>(); 
     }
     
     // Update is called once per frame
     void Update ()
     {
         ColorChange();
     }
     
     void ColorChange()
     {
         Debug.Log("Calling");
         fadeAmount += fadeSpeed * Time.deltaTime;
         txt.color = Color.Lerp(oldColor, newColor, fadeAmount);
         
         if (fadeAmount>1f)
         {
             fadeAmount = 0;
             oldColor = txt.color;
             int random = Random.Range(1, 5);
             
             if (random == 1)
                 newColor = Color.red;
             else if (random == 2)
                 newColor = Color.cyan;
             else if (random == 3)
                 newColor = Color.magenta;
             else if (random == 4)
                 newColor = Color.green;
             
         }
     }
 }
 
avatar image richyrich · Nov 12, 2014 at 06:09 PM 0
Share

Cool. Good luck with the project

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

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

Related Questions

How to make a spawner wait for x seconds? 2 Answers

Prefab Instantiation 1 Answer

How to Spawn multiple game object one by one in random order 1 Answer

Convert InvokeRepeating into the Update method... 1 Answer

Replay system for android, need help with precision (framerate problem) 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