Hi,
i’m a new to unity and trying to change a sprite’s color through a script.
i’m following this tutorial page: Unity - Coroutines
i’m using Unity 2019.2.0f1
this is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class colorChanger : MonoBehaviour
{
public int seconds_interval;
public SpriteRenderer sr;
public Color color_1;
public Color color_2;
IEnumerator changeColor(){
while (true){
if (sr.color == color_1){
sr.color = color_2;
} else {
sr.color = color_1;
}
yield return new WaitForSeconds(seconds_interval);
}
}
// Start is called before the first frame update
void Start()
{
sr = gameObject.GetComponent<SpriteRenderer>();
StartCoroutine( changeColor() );
//sr.color = color_1;
}
// Update is called once per frame
void Update()
{
}
}
this is the sprite, notice it starts as purple, then the script should change it’s color:
when pressing “play” i can see the colors changing every second in the inspector, but nothing happens in the scene:
yellow:
green (after 1 second):
actually, the inspector is totaly ignored by the scene! notice that the sprite is white (and not purple/yellow/green).
notice that if i change the scale nothing happens in the “play” screen:
but when i go back to “game” screen (without stopping the game), the frame of the sprite changes but not it’s actual size:
it looks like the script is making the inspector disconnect from the scene.
is it a bug? or am i missing something here?
thank you