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 nattsmilgg · Aug 31, 2020 at 10:01 PM · arraysui imageimage loader

Choose 4 random Images from a Sprite array and load them in premade Image containers without repeat selections?

Hello Unity brotherhood and sisterhood.

I have got a script that loads each Image but I get duplicate results. Can anyone suggest how to solve the duplication ?

I have looked into Fisher Yates Shuffle but I can't get it to work for this scenario.

This is the code that does load a random image on each Image container...

Any suggestions would be great! Thank you!

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class list : MonoBehaviour
 {
     // Image containers that get loaded with a random image from the Sprite array
     public Image image1;
     public Image image2;
     public Image image3;
     public Image image4;
 
     // Array 
     public Sprite[] images;
     
 
     void Start()
     {
         changeImage();
     }
 
     void changeImage()
     {
         // Load each Image container with a random selection from the Sprite array
 
         int num = UnityEngine.Random.Range(0, images.Length);
         image1.sprite = images[num];
         int num2 = UnityEngine.Random.Range(0, images.Length);
         image2.sprite = images[num2];
         int num3 = UnityEngine.Random.Range(0, images.Length);
         image3.sprite = images[num3];
         int num4 = UnityEngine.Random.Range(0, images.Length);
         image4.sprite = images[num4];
 
     }
 
 }
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
2
Best Answer

Answer by Hellium · Aug 31, 2020 at 10:19 PM

 using System;
 using System.Collections.Generic;
 using System.Security.Cryptography;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class list : MonoBehaviour
 {
     public List<Image> Images;
     public List<Sprite> Sprites;
 
     void Start()
     {
         AssignSprites(Shuffle(Sprites), Images);
     }
 
     void AssignSprites(IList<Sprite> sprites, IList<Image> images)
     {
         for (int i = 0 ; i < images.Count && i < sprites.Count ; ++i)
             images[i].sprite = sprites[i];
     }
 
     private static IList<T> Shuffle<T>(IList<T> list)
     {
         RNGCryptoServiceProvider provider = new RNGCryptoServiceProvider();
         IList<T> result = new List<T>(list);
         int n = result.Count;
         while (n > 1)
         {
             byte[] box = new byte[1];
             do provider.GetBytes(box);
             while (!( box[0] < n * ( Byte.MaxValue / n ) ));
             int k = (box[0] % n);
             n--;
             T value = result[k];
             result[k] = result[n];
             result[n] = value;
         }
         return result;
     }
 }

Comment
Add comment · Show 5 · 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 nattsmilgg · Sep 02, 2020 at 10:20 AM 0
Share

Thank you for your time!

After testing:

I have stored 10 sprites in the list and only first 4 get selected(on every start) instead of full list ?

p.s Those first 4 selected do get randomly placed into the 4 image containers though.

avatar image Hellium nattsmilgg · Sep 02, 2020 at 10:44 AM 0
Share

If you have 4 containers, you can't expect to have the 10 sprites displayed. And if you have 4 sprites, you can't expect the 4 to be displayed on the 10 containers without repetition.

avatar image nattsmilgg Hellium · Sep 02, 2020 at 10:47 AM 0
Share

I have 4 containers. And the list has 10 sprites.

So the problem is to randomly select 4 out of these 10 to be displayed without repetition.

Show more comments

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

138 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

Related Questions

Trying to assign UI images from an array with scriptable objects not working? 1 Answer

Image appear on click 0 Answers

Get fill amount up and down in a partern 0 Answers

In UI Images how can we radially fill in segments? 1 Answer

Having Trouble with Texture Tiling and offset values. Very specific case. 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