I'm creating some lines of many diffent rectangles. Both lines and rectangles are created by code. I do some logic with the rectangle's vertices and update that information in my instantiated rectangles. This works really well.
Then on each rectangle I have 2 scripts, one storing data and the other storing a corutine to make the rectangle flash or stop flashing when certain conditions are met after lines and rectangles move possitions.
It does not work, it might have to be done with some other outside scripts and even diccionaries or lists, but as I'm new to coroutines I would like to know if it's possible to do it from each instantiated object by simply checking those bool and int variables as they change during the game.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class rectanguloAnim : MonoBehaviour{
rectangulo rect; //script containing data from this rectangle
Color32 colOrig;
Material material;
IEnumerator currentFlashCoroutine;
void Start()
{
rect = GetComponent<rectangulo>();
colOrig = GetComponent<rectangulo>().colorOriginal;
material = GetComponent<Renderer>().material;
}
void Update()
{
// this bool and int change when the line and rect are interacted with
if (rect.isIntermediate == true || rect.activeDirection != 0)
{
if (currentFlashCoroutine != null)
{
StopCoroutine(currentFlashCoroutine);
}
currentFlashCoroutine = ChangeColor(material, colOrig, Color.white);
StartCoroutine(currentFlashCoroutine);
}
else
{
if (currentFlashCoroutine != null)
{
StopCoroutine(currentFlashCoroutine);
}
}
}
IEnumerator ChangeColor(Material toChange, Color32 startColor, Color32 endColor)
{
float t = 0;
float colorDuration = Random.Range(2.49f, 6f);
while (t < colorDuration)
{
float timeDuration = Random.Range(.55f, .95f);
if (t > timeDuration)
{
t = 0;
}
t += Time.deltaTime;
toChange.color = Color.Lerp(startColor, endColor, t / colorDuration);
yield return null;
}
}
}