How do you cycle through images in a panel? When my GO stops an Emoji icon appears on top of it and I would like it to change every other second until it gets moving again. Any tips?
public class Emotion : MonoBehaviour {
public GameObject Sphere;
public Canvas Emoji;
private bool hide = false;
private bool show = true;
private bool Tigil = false;
float CarSpeed;
Stop stop;
void PopUp()
{
if (stop.SPEED == 0) {
Emoji.GetComponent<Canvas> ().enabled = show;
Tigil = true;
} else if (Tigil)
{
Emoji.GetComponent<Canvas> ().enabled = hide;
Tigil = false;
}
}
// Use this for initialization
void Start () {
stop = Sphere.GetComponent<Stop>();
Emoji.GetComponent<Canvas> ().enabled = hide;
}
// Update is called once per frame
void Update () {
PopUp ();
}
}
You can use a float to count the time remaining to show next image and then when time remaining reaches zero, show next image. This needs to be done only when your character is at rest. Something like:
// Set your images to in this array.
public Sprite[] images;
private int m_CurrentImageIndex;
private float m_TimeRemaingNextImage = 0f;
// Cache your Image component in this in Start() method or assign it from Inspector.
private Image image;
void PopUp()
{
//-- Your current code goes here.
if(Tigil) {
m_TimeRemainingNextImage -= Time.deltaTime;
if(m_TimeRemainingNextImage <= 0f) {
image.sprite = images[GetImageIndex()];
m_TimeRemainingNextImage = 3f;
}
}
}
private int GetImageIndex()
{
m_CurrentImageIndex += 1;
return (m_CurrentImageIndex >= images.length) ? 0 : m_CurrentImageIndex;
}