So I have been trying making this game, which changes color of sprites in it. It should only happen when OnMouseDown(). but it is executing every frame and cannot go out of the couroutine. I want the color change to be slower. Please mark the colors should still change.
Please Help,
public class control : MonoBehaviour
{
public bool startstop = false;
SpriteRenderer m_SpriteRenderer;
private void Update()
{
StartCoroutine("Changecolor", 3f);
}
IEnumerator Changecolor()
{
yield return new WaitForSeconds(3);
if(startstop == true)
{
int random = Random.Range(1, 4);
if (random == 1)
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.blue;
}
else if(random == 2)
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.red;
}
else if(random == 3)
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.green;
}
else
{
m_SpriteRenderer = GetComponent<SpriteRenderer>();
m_SpriteRenderer.color = Color.yellow;
}
}
}
private void OnMouseDown()
{
startstop = !startstop;
}
}
startstop
variable istrue
. This means if you press the mouse button once,startstop
will betrue
and will keep changing the colours every frame. What would be the ideal result ? I'm not sure what you are trying to accomplish. Would you like sprites to change colour once every mouse click? Or just less often? \$\endgroup\$