Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 Sskethan · Jun 03, 2016 at 09:23 PM · 2d gamedice

2D dice rolling

Hello, I need some help with a 2D dice rolling script. There is 1 die and when clicked on the images flash randomly and when it stops the number of rolls is displayed. This is working, But i would like to add a total of the dice as they are displayed by there face value. I have tried as it shows in the script but not sure how to get a list of numbers 1 to 6 to correspond to the images 1 through 6 as they are displayed. Please let me know if anyone has any ideas. Thank you

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
  
 
 public class DiceRoll : MonoBehaviour
 {
     public Sprite[] Dice;
 
     private bool isRolling;
     private float totalTime;
     private float intervalTime;
     private int currrentDie;
     private bool dieRolled;
     private int currentTotal;
 
     public Text rollText;
     public int rolls;
     public int rollValue;
     public Text totalText;
     public int total;
     public int[] totalValue;
 
     Image die;
 
     void Start()
     {
         rolls = 0;
         total = 0;
         UpdateRolls();
         Init();
     }
 
     private void Init()
     {
         totalTime = 0.0f;
         intervalTime = 0.0f;
         currrentDie = 0;
         dieRolled = false;
         die = GameObject.Find("DieImage").GetComponent<Image>();
         die.sprite = Dice[currrentDie];
     }
 
     void Update()
     {
         if (isRolling)   
         {
             intervalTime += Time.deltaTime;
             totalTime += Time.deltaTime;
 
             if (intervalTime >= 0.1f)
             {
                 //change die & rotation
                 currrentDie = Random.Range(0, 6);
                 die.transform.Rotate(0, 0, Random.Range(0, 360));
 
                 //set image to selected die
                 die.sprite = Dice[currrentDie];
                 intervalTime -= 0.1f;
             }
 
             if (totalTime >= 2.00f)
             {
                 isRolling = false;
                 dieRolled = true;
                 AddRolls(rollValue);
                 AddTotal(totalValue);
             }
         }
     }
 
     public void DieImage_Click()
     {
         if (!dieRolled)
         {
             isRolling = true;  
         }
 
         if (!isRolling)
         {
             Init();   
             isRolling = true;
         }
     }
 
     public void PanelTestButton_Click()
     {
         GameObject panel = GameObject.Find("DiePanel");
         Animator animator = panel.GetComponent<Animator>();
         animator.Play("DiePanelOpen");
     }
 
     public void PanelOKButton_Click()
     {
         GameObject panel = GameObject.Find("DiePanel");
         Animator animator = panel.GetComponent<Animator>();
         animator.Play("DiePanelClose");
         rolls = 0;
         total = 0;
         UpdateRolls();
     }
 
     public void AddRolls(int newRollValue)
     {
         rolls += newRollValue;  
         UpdateRolls();
     }
 
     public void AddTotal(int[] newtotalValue)
     {
         total += newtotalValue[currentTotal];
         UpdateRolls();
     }
 
     void UpdateRolls()
     {
         rollText.text = "Rolls: " + rolls;
         totalText.text = "Total: " + rolls;
     }
 }
 
Comment
Add comment
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

1 Reply

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

Answer by TBruce · Jun 03, 2016 at 09:43 PM

You have a lot of variables that are never being set but you are using them. I have made some small mods to your code that hopefully will put you on track

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
  
 public class DiceRoll : MonoBehaviour
 {
     public Sprite[] Dice;
 
     public Text rollText;
     public int rolls;
     public int rollValue;
     public Text totalText;
     public int total;
     public int[] totalValue;
 
     private bool isRolling;
     private float totalTime;
     private float intervalTime;
     private int currrentDie;
     private bool dieRolled;
     private int currentTotal;
 
     Image die;
 
     void Start()
     {
         rolls = 0;
         total = 0;
 
         // moved this here because there is no need  to continually get the same object
         die = GameObject.Find("DieImage").GetComponent<Image>();
         UpdateRolls();
         Init();
     }
 
     private void Init()
     {
         totalTime = 0.0f;
         intervalTime = 0.0f;
         currrentDie = 0;
         dieRolled = false;
         die.sprite = Dice[currrentDie];
     }
 
     void Update()
     {
         if (isRolling)   
         {
             intervalTime += Time.deltaTime;
             totalTime += Time.deltaTime;
 
             if (intervalTime >= 0.1f)
             {
                 //change die & rotation
                 currrentDie = Random.Range(0, 6);
                 die.transform.Rotate(0, 0, Random.Range(0, 360));
 
                 //set image to selected die
                 die.sprite = Dice[currrentDie];
                 intervalTime -= 0.1f;
             }
 
             if (totalTime >= 2.00f)
             {
                 isRolling = false;
                 dieRolled = true;
                 AddRolls(rollValue);
                 // removed the parameter because AddTotal() is called from wone place with same parameter
                 AddTotal();
             }
         }
     }
 
     public void DieImage_Click()
     {
         if (!dieRolled)
         {
             isRolling = true;  
         }
 
         if (!isRolling)
         {
             Init();   
             isRolling = true;
         }
     }
 
     public void PanelTestButton_Click()
     {
         GameObject panel = GameObject.Find("DiePanel");
         Animator animator = panel.GetComponent<Animator>();
         animator.Play("DiePanelOpen");
     }
 
     public void PanelOKButton_Click()
     {
         GameObject panel = GameObject.Find("DiePanel");
         Animator animator = panel.GetComponent<Animator>();
         animator.Play("DiePanelClose");
         rolls = 0;
         total = 0;
         UpdateRolls();
     }
 
     public void AddRolls(int newRollValue)
     {
         rolls += newRollValue;  
         UpdateRolls();
     }
 
     // removed the parameter because AddTotal() is called from wone place with same parameter
     public void AddTotal()
     {
         // value is the true amount of the die face (currrentDie is 0 base so add 1)
         int value = currrentDie + 1;
         totalValue[currrentDie] += value; // add value to totalValue of current die
         total += value;                   // add value to total
         UpdateRolls();
     }
 
     void UpdateRolls()
     {
         rollText.text = "Rolls: " + rolls;
         totalText.text = "Total: " + rolls;
     }
 }
Comment
Add comment · Show 1 · 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 Sskethan · Jun 03, 2016 at 10:02 PM 0
Share

Thank you. It's working great now.

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.

Follow this Question

Answers Answers and Comments

61 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

Related Questions

How to make the 2D board game piece move only when the player answers the question correctly? 0 Answers

How to rotate sword while still looking at mouse? 1 Answer

How do I make tank capitellar tracks In 2d? 0 Answers

Drop item based on die roll 1 Answer

Grass wave shader or other ideas 2 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