- Home /
Cannot seem to randomly generate my colors on my images
Hi!
I seem to have a problem with randomly generating my images. What I'm trying to achieve is that when the game starts, the images on my popup would take a sprite with a color on it and apply it to the image, so when I open the pop up, my images have the sprites with colors on them, randomly. Here's my code:
public class PopupPuzzle : MonoBehaviour { [SerializeField] private Text _text; [SerializeField] private GameObject _popUpBox; [SerializeField] private Animator _anim; [SerializeField] private AudioClip _clip;
//pour les couleurs randoms
[SerializeField] private Image[] _tableauCouleurs;
[SerializeField] private Sprite[] _couleurBleu;
[SerializeField] private Sprite[] _couleurRouge;
[SerializeField] private Sprite[] _couleurVert;
[SerializeField] private Sprite[] _couleurMauve;
[SerializeField] private Sprite[] _couleurJaune;
private List<Sprite[]> _listeCouleurs;
private AudioSource _audio;
public void PopUp(string text){
_popUpBox.SetActive(true);
_text.text = text;
_anim.SetTrigger("pop");
}
void Start(){
_listeCouleurs = new List<Sprite[]>();
_listeCouleurs.Add(_couleurBleu);
_listeCouleurs.Add(_couleurRouge);
_listeCouleurs.Add(_couleurVert);
_listeCouleurs.Add(_couleurMauve);
_listeCouleurs.Add(_couleurJaune);
InitCouleur();
_audio = GetComponent<AudioSource>();
_text.enabled = false;
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
_audio.PlayOneShot(_clip);
_anim.SetBool("Open", true);
_anim.SetBool("Close", false);
StartCoroutine("EnableText");
}
}
IEnumerator EnableText(){
yield return new WaitForSeconds (0.3f);
_text.enabled = true;
}
private void OnTriggerExit2D(Collider2D other)
{
if (other.CompareTag("Player"))
{
_text.enabled = false;
_anim.SetBool("Open", false);
_anim.SetBool("Close", true);
}
}
void Update(){
}
private void InitCouleur(){
for(int i = 0; i < _tableauCouleurs.Length; i++)
{
int _aleatoireCouleur = Random.Range(0, _listeCouleurs.Count);
Sprite[] n = _listeCouleurs[_aleatoireCouleur];
_tableauCouleurs[i].sprite = n[_aleatoireCouleur];
_listeCouleurs.RemoveAt(_aleatoireCouleur);
}
}
}
And here is an image with the unity editor in it: ][1]
Any help is EXTREMELY appreciated and I thank each and everyone for taking you time to help.
Answer by Zaeran · Dec 14, 2021 at 02:43 AM
It looks like your InitCouleur() function is either going to give you a blue colour, or throw an OutOfRangeException error.
First off, This will give you a number between 0 and 4.
int _aleatoireCouleur = Random.Range(0, _listeCouleurs.Count);
This then selects the associated Sprite array
Sprite[] n = _listeCouleurs[_aleatoireCouleur];
Now the problem:
_tableauCouleurs[i].sprite = n[_aleatoireCouleur];
If the '_aleatoireCouleur' is 0, you'll get the 1st Sprite in the 1st Sprite array (Blue), which in this case is the blue one. If '_aleatoireCouleur' is 1, you'll get the 2nd sprite in the 2nd sprite array (Red). The red sprite array also only has one value, so '_couleurRouge[1]' doesn't exist to pull the sprite from.
You'll either need to change them to single sprite variables instead of arrays, or change
_tableauCouleurs[i].sprite = n[_aleatoireCouleur];
to
_tableauCouleurs[i].sprite = n[0];
Yes Thank you! While waiting for an answer it seems I have the same solution! I thank you very much!