I’m working on a 2D dice game. Most of the code was finished and working on Android Studio, but as I’m willing to make it an online real time multiplayer I ended up buying a unity course as I believe unity is more appropriate for this purpose.
I’m still kinda lost though. I don’t need the game to roll 3D dice, I just want to recreate my 2D dice game in unity.
Am I correct to assume that the best way to do it is by creating 6 game objects (6 dice) and then change the image sprite source each time I roll the dice according to the random numbers I generate? That’s what I was doing on Android Studio with Java. I just have no idea how to do this on unity. Any advice, suggestion or piece of code are welcome!
var img1;Texture2D;
var img2;Texture2D;
var img3;Texture2D;
var img4;Texture2D;
var img5;Texture2D;
var img6;Texture2D;
var imgs:Texture2D[];
imgs=new Texture2D[7];
imgs[1]=img1;
imgs[2]=img2;
imgs[3]=img3;
imgs[4]=img4;
imgs[5]=img5;
imgs[6]=img6;
var dice1:GameObject;
var dice2:GameObject;
var i:int;
dice1=GameObject.Find("dice1");
dice2=GameObject.Find("dice2");
function Update () {
if(Input.GetKeyDown("space")){
i=Random.Range(1,7);
dice1.transform.renderer.material.mainTexture=imgs*;*
i=Random.Range(1,7); dice2.transform.renderer.material.mainTexture=imgs*;}* } make 2 planes or cubes Objects in your inspector and name them “dice1” and “dice2” in your scene. draw six PNG pictures and drag and drop them onto the texture spaces shown in the inspector from this code. press play … press spacebar:) i hope this example helps!
Hey, that’s great! It does work! I just don’t see any results in the game window when I play it. I thought it would get the image resource and add it to my game objects.
Btw, I would like to actually understand your code, do you know any good place for information on that?