OK, so I’m confused…can a button display a random image when clicked? What I mean is the image or icon that appears on the button, is that possible?
if possible, how?
OK, so I’m confused…can a button display a random image when clicked? What I mean is the image or icon that appears on the button, is that possible?
if possible, how?
To continue with @ZY_bros’ answer, once you manage to change the background image of the button after clicking on it.
You can now create an array where you will put all the image you want to randomly switch between.
Each time you click on the button, make a random from 0 to the array length (be careful about if the random function is inclusive or exclusive for the max value)
After getting a random number you can now set the background with yourArray[randomNumber]
I hope I have been understandable
Don’t exactly understand what you are saying, but if you want the button to show an image when clicked you can just use gameObject.SetActive(true)
whenever you click the button
However, if you are talking about changing the “background” for a button, you can just click on the button object, find “image”, then set target graphic to whatever you want.
Under the button attach this and when pressed execute ChangeImage()
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class ButtonChangeImage : MonoBehaviour
{
public Sprite[] randomImage;
private Image currentImage;
private int randomNum;
void Start()
{
currentImage = GetComponent<Image>();
}
public void ChangeImage()
{
randomNum = Random.Range(0, 3);
currentImage.sprite = randomImage[randomNum];
}
}