0

I have a public array full of Images from the inspector. A method runs which for 6 iterations, should generate a random number which links to an index in the array and makes that image visible.

When I run the program however, nothing actually happens, all images remain invisible. Can anyone identify the issue?

This is my code:

 public class CoinDecider : MonoBehaviour {
 public Image[] Images; //array of images, assigned through inspector

 public void Start()
 {
     //for 6 iterations, a random number between 1 and 16 is generated
     //that number is assigned to the index of one of the images
     //said image becomes visible
     for (int i = 0; i < 6; i++) 
     {
         int number = Random.Range(1, 16);
         var selected = Images[number];
         selected.enabled = true;
     }

     }
 }

Just for extra insurance, this is what the list from the inspector looks like (all images are placed on screen but defaulted to invisible at the start of the game): List of images from the inspector

Grease
  • 1
  • 2
  • Your posted code is a little confusing. Are there 16 images to select from? In addition, inside the `for` loop, the code “selects” one of the 16 images and then sets its `enabled` property to `true`? This is doing NOTHING with the `selected` image like assigning it to something. In its current state, the `for` loop is accomplishing nothing. It sets a local variable `selected` to an image, sets that image to enabled, then moves on to the next iteration which will overwrite the previous `selected` variable. Can you clarify any of this? – JohnG Mar 03 '21 at 01:50
  • Yes there are 16 images that I want the for loop to select from. All I want it to do is turn 6 images visible (since all images are initialised to be invisible at the start). It over-writing is intended. The for loop generates a random number, that number is then declared as the index of one of the images in the array through "selected" which then turns the image visible. This process then repeats 5 times. It gets overwritten with a new value, but the previous ones image has already been enabled, which is all it's meant to be doing. That being said, nothing gets enabled, which is the issue – Grease Mar 03 '21 at 01:57
  • Have you considered removing the unneeded variable `selected` and simply do… `Images[number].Enabled = true` ? Also, I assume the same image can be used more than once? I am confident that using … `int number = Random.Range(1, 16);`… inside the loop is going to have a high chance of randomly picking the “SAME” number as `Random` is re-initialized with each iteration. Have you checked to see if you get “different” random numbers with each iteration? – JohnG Mar 03 '21 at 02:11
  • I just tried the code amendment you suggested and still nothing is being enabled. Also I do realise there's a high chance of repeat (which I'll fix later) but even on tests where all 6 results are different, none of the images activate. – Grease Mar 03 '21 at 02:15
  • Sorry! I fixed it now. I've just realised there's no EventSystem within the new scene. I'm new to unity so a complete beginner's error. Thanks for the improvement on the code though! – Grease Mar 03 '21 at 02:21

0 Answers0