- Home /
If statement not being fullfilled until GameObject inspected in inspector.
I have a bit of code that alternates the target color of a Color.lerp if that Color.Lerp reaches it's target color. I have it working on some GameObjects, but for others, it gets to first target color, and will only update and begin to continously switch when I inspect it in the hierarchy. Any advice?
public IEnumerator RainbowIdle()
{
for (int i = 0; i < gameObject.transform.childCount; i++)
{
transform.GetChild(i).gameObject.GetComponent<SpriteRenderer>().color = Color.Lerp(transform.GetChild(i).gameObject.GetComponent<SpriteRenderer>().color, rainbow[currentColor], Time.deltaTime / idleLength);
}
if (transform.GetChild(0).gameObject.GetComponent<SpriteRenderer>().color == rainbow[currentColor])
{
if (currentColor == rainbow.Length - 1)
{
currentColor = 0;
}
else
{
currentColor += 1;
}
}
yield return null;
StartCoroutine(RainbowIdle());
}
There's nothing in what you've shown us that looks like it'll be dependent on whether or not something is currently selected in the editor. That's more likely to be to do with how you're starting this process (which you haven't shown us).
Having said that, the code as stands is very confusing. The fact that your coroutine only runs for one frame and then restarts itself is frankly a bit odd. And specifically 1) you're always lerping between the current colour and the target colour (so you don't have a linear change) 2) your end point relies on floating point equality which is dangerous (and may be related to why your code isn't moving on, if that's the if-statement you're referring to): if you want a process to take a particular amount of time, it's much better to use that as your endpoint (and with an inequality test rather than equality).
Your code is also very bad performance-wise as you are doing lots of unnecessary calls to GetComponent and Lerp. I couldn't bring myself to use it as a starting point... use variables and functions!
I would suggest that something along these lines would make more sense. Note that I'm not making any function calls in the block of code that gets called every frame during the lerping (ie the while loop), beyond those that absolutely need to be called.
public float changeColourDuration;
public Color[] rainbow;
SpriteRenderer[] rendArray;
int currentColour = 0;
Start()
{
rendArray = transform.GetComponentsInChildren<SpriteRenderer>();
SetAllColoursTo(0);
StartCoroutine( LerpAllColoursTo( 1);
}
void SetAllColoursTo(int i)
{
for (int i = 0; i < rendArray.Length; i++)
{
rendArray[i].color = rainbow[i];
}
}
IEnumerator LerpAllColoursTo(int i)
{
Color startColour = rendArray[currentColour].color;
Color endColour = rendArray[i].color;
float elapsed = 0f;
while (elapsed < changeColourDuration)
{
elapsed += Time.deltaTime;
SetAllColoursTo( Color.Lerp( startColour, endColour, elapsed/changeColourDuration);
yield return null;
}
currentColour = i;
StartCoroutine( LerpAllColoursTo( (i+1) % rainbow.Length );
}
Your answer
Follow this Question
Related Questions
lerp transparency back and forth, while cycling through rainbow? 0 Answers
Lerp to position, after a while come back, problem occured using Coroutines 1 Answer
Lerp a light intensity gradually to 0 1 Answer
restrict Lerp to only one axis? 1 Answer
Lerping multiple materials of a gameobject within a coroutine 0 Answers