I have a gallery Image where i have 4 UI images choose random sprite form my sprite array and assign it after a random time. My issue is that my 4 UI images are repeating for example Image 1 will display House, there is also a change for Image 2 , 3 or 4 to display the same image. I don't want that. my gallery script is attached to each UI image there for i have 4 UI images so i have 4 instances of the script. issue is that the UI images doesn't Know about each other therefore i tried to get component of Gallery Script component so I can get the value and not make it choose it but sadly it didn't work. Could someone Help me? Here's my Gallery script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Gallery : MonoBehaviour
{
public float timer;
public Sprite[] gallerySprites;
public Image currentImage;
public int newRandomNummer;
int lastRandomNumber;
int min = 0;
int max;
public Gallery[] galleries;
public List<int> val = new List<int>();
// Use this for initialization
void Start()
{
max = gallerySprites.Length;
timer = MakeRandomNumber(8, 18);
}
// Update is called once per frame
void Update()
{
timer -= Time.deltaTime;
if (timer <= 0)
{
StartCoroutine(fadeImages());
timer = MakeRandomNumber(8, 18);
}
}
IEnumerator fadeImages()
{
currentImage.CrossFadeAlpha(0, 1f, false);
yield return new WaitForSeconds(1f);
currentImage.CrossFadeAlpha(1, 1f, false);
currentImage.sprite = gallerySprites[MakeRandomNumber(0, max)];
}
int MakeRandomNumber(int min , int max)
{
newRandomNummer = Random.Range(min, max);
for (int i = 0; i < galleries.Length; i++)
val.Add(i);
if (newRandomNummer != lastRandomNumber)
{
Debug.Log("new Value");
return newRandomNummer;
}
else
{
//retry!
newRandomNummer = Random.Range(min, max);
return newRandomNummer;
}
}
}
Aucun commentaire:
Enregistrer un commentaire