- Home /
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.
You could just check the time and if not enough has passed, don't execute the function
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 ?
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
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;
}
}
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.
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;
}
}
}