Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Mitrovic_Z · Dec 14, 2021 at 02:14 AM · scripting problemspriteimage

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: ![alt text][1]

Any help is EXTREMELY appreciated and I thank each and everyone for taking you time to help.

screenshot-1.png (277.8 kB)
screenshot-1.png (277.8 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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];
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Mitrovic_Z · Dec 14, 2021 at 02:44 AM 0
Share

Yes Thank you! While waiting for an answer it seems I have the same solution! I thank you very much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

259 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

AR Foundation Image Tracking and Markers 0 Answers

How to set a prefab SpriteRenderer.sprite using script in editor? 1 Answer

Change sprite vs sprite animation 0 Answers

Resource load default unity sprite? 1 Answer

Why is the alpha on my UI Image not decreasing properly? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges