• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by not_Dylon · Mar 14, 2016 at 07:06 PM · spritegameobjects

2D dice game!

Hey guys!

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!

Thanks in advance!

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Mar 14, 2016 at 09:02 PM 0
Share

do you have screenshots of your game? do you need anytype of animations for rolling and stuff?

avatar image not_Dylon toddisarockstar · Mar 14, 2016 at 09:49 PM 0
Share

Not for now! I wanna focus on making the game playable before start working on animations. I just need to roll 6 dice, according to the numbers rolled ( I already wrote the code to roll it ), I want to display an img for each of the 6 dice with the corresponding face value. Then select the ones I want to remove and roll again the rest of the dice.

thanks for your reply! :)

3 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by toddisarockstar · Mar 14, 2016 at 10:08 PM

 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];
 i=Random.Range(1,7);
 dice2.transform.renderer.material.mainTexture=imgs[i];}
 }


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!

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image not_Dylon · Mar 14, 2016 at 11:08 PM 0
Share

That's awesome, thanks man. That is js, right? I never studied this language but I get the idea. I still have to figure some stuff out before I can make it work.

I created 2 planes and named them dice1 and dice2. (check)

I have the PNG pictures. What I don't know is:

In my dice1 I clicked add component and added a javascript. I tried placing your code everywhere. First I left everything out of the start and update functions but the if statement that I placed inside the update function. It didnt work. then I tried all outside. Then tried all in the update, etc... I always get this error on unity.

"Expressions in statements must only be executed for their side effects"

What am I doing wrong? Cause I don't even get thes texture spaces in the inspector...

Sorry if I'm being inconvenient.

avatar image toddisarockstar not_Dylon · Mar 15, 2016 at 01:08 AM 0
Share
      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.Get$$anonymous$$eyDown("space")){
      i=Random.Range(1,7);
      dice1.transform.renderer.material.mainTexture=imgs[i];
      i=Random.Range(1,7);
      dice2.transform.renderer.material.mainTexture=imgs[i];}
      }


Oops i had a typo in my script. i didnt test it early but i did now. start with a completely empty Js script and paste this in now. it should work! actually it will work on anywhere in your game if you attach it to anything!

avatar image toddisarockstar not_Dylon · Mar 15, 2016 at 01:13 AM 0
Share
  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.Get$$anonymous$$eyDown("space")){
  i=Random.Range(1,7);
  dice1.transform.renderer.material.mainTexture=imgs[i];
  i=Random.Range(1,7);
  dice2.transform.renderer.material.mainTexture=imgs[i];}
  }

ooops. sorry i had a small typo. i didnt test it earlier but i did now. you should be able to start with a completely empty javascript and paste this in as is and it should work now. you can attach it once to any object, anywhere in your scene! let me know if you get it going!

avatar image
0

Answer by not_Dylon · Mar 15, 2016 at 10:24 PM

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?

Thanks again!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image toddisarockstar · Mar 15, 2016 at 10:55 PM 0
Share

if you look in the inspector when you select the gameobject where you attached the script, the code should leave 6 blank spaces for textures.

I figures you could draw some images with paint or something that would look like the dice faces. if put those images in your assetts folder you can simply drag and drop them into the bank spaces in the inspector and this code will automatically resign those images on the dice objects according to the dice rolls.

the difference translating between C# and javascript is usually simple. ins$$anonymous$$d of saying "viod" you say "function". when decaring variables in javascript you say "var". there are some other differences but basically it all works the same. all the unity commands are the same.

the code might look a bit confusing if you are not familiar with arrays or lists. whatever images you "drag and drop" gets packed into an array of textures for easy access. the code then simply grabs a random number and assigns the corresponding texture number to the dice objects when you roll.

if you are not fimiliar with unity coding there is usually a process of declairing empty variables at the top of your script. some might be images. some are game objects. some are audio samples and so on. the easiest way to give the script the item is to put it in your asset folder and then drag and drop in the inpector so your code knows what it is and can do stuff with it!

avatar image not_Dylon toddisarockstar · Mar 15, 2016 at 11:07 PM 0
Share

Hey, toddisarockstar.

Thanks again for your reply.

I did it before, I added dice face sprites to my project and dragged them into those blank spaces. But when I play the game and press space I can see a ball (yes, a sphere shaped "die") that changes according to the number generated every time I press space. But it's not on my Game window, it shows up in the inspector and nothing shows up in the game window.

And I get the whole code syntax, what I don't get is the unity part of the syntax, such as : dice1.transform.renderer.material.mainTexture.

I know what its doing, I'm just wondering where I could get more info regarding that.

Thank you!

avatar image toddisarockstar not_Dylon · Mar 15, 2016 at 11:37 PM 0
Share

eh could be something about where your camera is or the lighting not turned on or mabe a component missing. idk. anyways to explain how unity works....

when you say dice1 you are telling what game object.

when you say transform you are asking for everything listed in the inspector.

then when you ask for renderer you ask for access to the component you see in the inspector that handles texture.

when you say material you are accessing the material under the renderer.

when you say maintexture. you are changing the actually image in that texture!

about every variable in the components you see in the inspector can be changed through script.

under material there is also variables to change color, the way they cast shadow, how materials reflect light how translucent they are and so on. the maintexture is a common one.

the same formats go for changing things like position and rotation and stuff.

you eventually end up memorizing the common ones and get used to using the autocomplete suggestions when you start typing on monodevelp. seems google directs you very quickly to the scripting API that quickly tells you how to access something your not familiar with. the more you get fimiliar with looking up how to change stuff the faster you get at coding.

the best way to think of it is that you are basically changing all the numbers in the inpector with script with code!

avatar image JWong · Sep 03, 2016 at 05:24 AM 0
Share

What would this look like in C#?

avatar image
0

Answer by zaujas · Jan 25, 2017 at 01:24 PM

You could solve the problem of not seeing. I have the same problem.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

63 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Scale Sprite like UI Canvas 1 Answer

TextureImporterType.Sprite does not work 0 Answers

I draw a sprite and i cant put sprites in unity. 0 Answers

Why am I getting this error "Object reference not set to an instance of an object" 1 Answer

How can I apply a 9-slice scaling to a mash object using a shader? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges