Need help on creating a 2D dice. On clicking a button a random no must be generated (1-6) and depending upon the number the corresponding dice sprite must be displayed instead of the button or as a new image nearby. I’ve browsed though many questions like this and I’m not able to get it. Also, Under the Inspector menu for my button, I’ve set the transition to sprite swap and added the respective On Click() functions. Any help would be much appreciated. Here’s my code:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class RandNumGen : MonoBehaviour {
public int RandNum;
public Sprite white1;
public Sprite white2;
public Sprite white3;
public Sprite white4;
public Sprite white5;
public Sprite white6;
//private int swcase;
private SpriteRenderer sr;
void Start () {
sr = this.GetComponent<SpriteRenderer> ();
}
// Use this for initialization
public void StartX () {
RandNum = Random.Range (1, 7);
}
// Update is called once per frame
public void UpdateX(){
Debug.Log ("Generated Num :" + RandNum);
ChangeSprite ();
}
public void ChangeSprite(){
switch (RandNum)
{
case 1:
sr.sprite = white1;
break;
case 2:
sr.sprite = white2;
break;
case 3:
sr.sprite = white3;
break;
case 4:
sr.sprite = white4;
break;
case 5:
sr.sprite = white5;
break;
case 6:
sr.sprite = white6;
break;
default:
Debug.Log("Not Working");
break;
}
}
}