[Help]Randomly Selecting a Background Image from an Array of Backgrounds
 Hey everyone, I am trying to script the selection of a random background image on my canvas for a simple number guessing game (2D) from an array of background images. I've tried a couple of things so far. Here is my current attempt:
Hey everyone, I am trying to script the selection of a random background image on my canvas for a simple number guessing game (2D) from an array of background images. I've tried a couple of things so far. Here is my current attempt:
 private Image background;
 [SerializeField]
 private List<Image> bgImage = new List<Image>();
 void Start ()
 {
     background = GetComponent<Image>();
     Image randomBG = bgImage[Random.Range(0, bgImage.Count)];
     background = randomBG;
 }
 
In this attempt, I am not able to add my Images to my Array in Unity. I have also tried using Sprite instead but that doesn't seem to change the background as desired. Any help is appreciated.
Answer by kingdomseed · Oct 14, 2016 at 03:32 PM
Got it!!! Switched the Array to a Sprite and was able to change the sprite of the background.
 private Image background; [SerializeField] private List<Sprite> bgImage = new List<Sprite>(); void Start () { background = GetComponent<Image>(); Sprite randomBG = bgImage[Random.Range(0, bgImage.Count)]; background.sprite = randomBG; } 
Your answer
 
 
             Follow this Question
Related Questions
Background in 2D game: sprite or image? 0 Answers
Hotbar appears when a certain gameobject is selected? 1 Answer
How to fit a picture into a background GameObject 0 Answers
How to move a UI Image in the z axis? 2D 0 Answers
How to create a zoomable canvas image in Unity for mobile without scaling the main camera? 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                