Question by
Xenogrease · Mar 03, 2021 at 12:48 AM ·
c#arrayrandom.rangeimages
How to get images from an array visible
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. 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;
}
}
}
Comment
Is the script attached to an active gameObject?
You call
selected.enabled
which enables the Image component, not the gameObject so ensure that the gameObjects are active, but their Image components are not