add total of 2 dice in C#

using UnityEngine;
using System.Collections;

public class DiceSwipeControl : MonoBehaviour
{
        // Static Instance of the Dice
        public static DiceSwipeControl Instance;
    #region Public Variables
        // Orignal Dice
        public GameObject die1;
        //dice resultant number..
        public int diceCount;
        public int diceCount1;
        //dice play view camera...
        public Camera dicePlayCam;
        //Can Throw Dice
        public bool isDiceThrowable = true;
        public GUIText gui;
        public Transform diceCarrom;
    #endregion

    #region Private Varibles
        private GameObject diceClone;
        private GameObject diceClone1;
        private Vector3 initPos;
        private float initXpose;
        private float timeRate;
        // To Save Camera Postion
        private Vector3 currentCampPos;
        Vector3 objectPos;
        internal float diceThrowInit;
    #endregion

        void Awake ()
        {
                Instance = this;

        }

        void Start ()
        {
                generateDice ();
        }

        void Update ()
        {
                if (isDiceThrowable)
                {
                    if (Input.GetMouseButtonDown (0))
                    {
                            initPos = Input.mousePosition;
                            initXpose = dicePlayCam.ScreenToViewportPoint (Input.mousePosition).x;
                    }
                    Vector3 currentPos = Input.mousePosition;
                    currentPos.z = 25f;
                    Vector3 newPos = dicePlayCam.ScreenToWorldPoint (new Vector3(currentPos.x,currentPos.y,Mathf.Clamp(currentPos.y/10,5,70)));
                    newPos.y = Mathf.Clamp(newPos.y, -114.5f, 100);
                    newPos = dicePlayCam.ScreenToWorldPoint (currentPos);
                    if (Input.GetMouseButtonUp (0))
                    {
                        initPos = dicePlayCam.ScreenToWorldPoint (initPos);
      
                        enableTheDice ();
                        addForce (newPos);
                        isDiceThrowable = false;
                              
                        StartCoroutine (getDiceCount ());
                    }
                }
        }

        void addForce (Vector3 lastPos)
        {
                diceClone.GetComponent<Rigidbody>().AddTorque(Vector3.Cross(lastPos, initPos) * 2000, ForceMode.Impulse);
                lastPos.y += 12;
                diceClone.GetComponent<Rigidbody>().AddForce (((lastPos - initPos).normalized) * (Vector3.Distance (lastPos, initPos)) * 40 * diceClone.GetComponent<Rigidbody>().mass);

                diceClone1.GetComponent<Rigidbody>().AddTorque(Vector3.Cross(lastPos, initPos) * 2000, ForceMode.Impulse);
                lastPos.y += 12;
                diceClone1.GetComponent<Rigidbody>().AddForce (((lastPos - initPos).normalized) * (Vector3.Distance (lastPos, initPos)) * 40 * diceClone1.GetComponent<Rigidbody>().mass);
    }
  
        void enableTheDice ()
        {      
                diceClone.transform.rotation = Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180));
                diceThrowInit = 2;

                diceClone1.transform.rotation = Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180));
                diceThrowInit = 2;
    }

        void generateDice ()
        {
                diceClone = Instantiate (die1, dicePlayCam.transform.position, Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180))) as GameObject;
                diceClone1 = Instantiate (die1, dicePlayCam.transform.position, Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180))) as GameObject;
        }


    #region Coroutines

        IEnumerator getDiceCount ()
        {
                //Time.timeScale = 1.3f;  
                currentCampPos = dicePlayCam.transform.position;
                //wait fore dice to stop...
                yield return new WaitForSeconds (1.0f);
                while (diceClone.GetComponent<Rigidbody>().velocity.magnitude > 0.05f) {
                        yield return 0;

            }

                Time.timeScale = 0.2f;
                timeRate = 0.001f;
                float startTime = Time.time;
                Vector3 risePos = dicePlayCam.transform.position;
                Vector3 setPos = new Vector3 (diceCarrom.position.x, diceClone.transform.position.y + 25f, diceCarrom.position.z);
                float speed = 0.18f;
                float fracComplete = 0;

                while (Vector3.Distance(dicePlayCam.transform.position,setPos)>0.5f)
                {
                        Vector3 center = (risePos + setPos) * 0.5f;
                        center -= new Vector3 (0, 2, -1);
                        Vector3 riseRelCenter = risePos - center;
                        Vector3 setRelCenter = setPos - center;
              
                        if (fracComplete > 0.85f && fracComplete < 1f) {
                                speed += Time.deltaTime * 0.3f;
                                Time.timeScale -= Time.deltaTime * 4f;
                        }
                        dicePlayCam.transform.position = Vector3.Slerp (riseRelCenter, setRelCenter, fracComplete);
                        dicePlayCam.transform.position += center;
                        dicePlayCam.transform.LookAt (diceCarrom);
                        fracComplete = (Time.time - startTime) / speed;
                        yield return 0;
                }

                Time.timeScale = 1.0f;
                Dice.Instance.GetDiceCount ();
                gui.text = "Dice Count : " + Dice.Instance.GetDiceCount();
                yield return new WaitForSeconds (5f);

                Dice.Instance.diceCount = 0;
                Dice.Instance.GetDiceCount ();
                diceCount = Dice.Instance.GetDiceCount ();
                gui.text = "Dice Count : " + diceCount;
                diceClone.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
                Application.LoadLevel(0);
        }

    #endregion
}
using UnityEngine;
using System.Collections;

public class Dice : MonoBehaviour
{
        public int diceCount;
        public int diceCount1;
        public static Dice Instance;
        internal Vector3 initPos;
        void Start ()
        {
               GetComponent<Rigidbody>().solverIterationCount = 250;
                Instance = this;
                initPos = transform.position;
        }

        void OnEnable()
        {
            initPos = transform.position;
        }

        public int GetDiceCount ()
        {
                regularDiceCount ();
                return diceCount;
        }

        void regularDiceCount ()
        {
                if (Vector3.Dot (transform.forward, Vector3.up) > 0.6f)
                    diceCount = 5 + diceCount1;
                if (Vector3.Dot (-transform.forward, Vector3.up) > 0.6f)
                    diceCount = 2 + diceCount1;
                if (Vector3.Dot (transform.up, Vector3.up) > 0.6f)
                    diceCount = 3 + diceCount1;
                if (Vector3.Dot (-transform.up, Vector3.up) > 0.6f)
                    diceCount = 4 + diceCount1;
                if (Vector3.Dot (transform.right, Vector3.up) > 0.6f)
                    diceCount = 6 + diceCount1;
                if (Vector3.Dot (-transform.right, Vector3.up) > 0.6f)
                    diceCount = 1 + diceCount1;
       
                if (Vector3.Dot (transform.forward, Vector3.up) > 0.6f)
                    diceCount1 = 5;
                if (Vector3.Dot (-transform.forward, Vector3.up) > 0.6f)
                    diceCount1 = 2;
                if (Vector3.Dot (transform.up, Vector3.up) > 0.6f)
                    diceCount1 = 3;
                if (Vector3.Dot (-transform.up, Vector3.up) > 0.6f)
                    diceCount1 = 4;
                if (Vector3.Dot (transform.right, Vector3.up) > 0.6f)
                    diceCount1 = 6;
                if (Vector3.Dot (-transform.right, Vector3.up) > 0.6f)
                    diceCount1 = 1;
        }
   

   
}
using UnityEngine;
using System.Collections;

public class LoadStartScene : MonoBehaviour
{
    public int timeToComplete = 3;

    // Use this for initialization
    void Start () {
        //Use this to Start progress
            StartCoroutine(RadialProgress(timeToComplete));
    }

    IEnumerator RadialProgress(float time)
    {
        float rate = 1 / time;
        float i = 0;
        while (i < 1)
        {
            i += Time.deltaTime * rate;
            gameObject.GetComponent<Renderer>().material.SetFloat("_Cutoff", i);
            yield return 0;
        }
    }
}

2149648–141865–Dice.cs (1.56 KB)
2149648–141866–DiceSwipeControl.cs (4.88 KB)
2149648–141867–LoadStartScene.cs (592 Bytes)

please use insert code

It is impossible to read like that

I am try to post it with the formatting but my first time posting here so i added the .cs file. I am try now to edit it to read with formatting to make it readable. Thanks for the response and for looking at it.

try this

Thanks a lot for the link to post properly. Guess thats the problem with my code I dont research deep enough or I would have known this before I posted. What I am try to do is get the total of the 2 dice in the text.

Ok, it looks way too complicated for what you are trying to achieve.

Still I am not sure which is your problem. I would go for a Dice object, that way you can have N dice Instances while Update only Sums.

From what I saw I would change Dice.cs

diceCount and diceCount1 are the same number (X*2 instead of X+diceCount1), at the same time you might get more than one clause right so it might need else if instead of if.

Also, the first run diceCount1 will always be 0.

diceThrowInit is never used in DiceSwipeControl even that you give it the same value twice in the same function.

So, explain your problem and someone might be able to give you a better help.

Thanks for the reply I am about to try to implement the process you explained. The problem at this point is I am making a game of “Craps” like at the Casino. The place that got my stuck is I got the one dice (diceCount) to show the total but then added the (diceCount1) to get the 2 dice to total up to a sum. Read up and changed it now like 50 times and cant figure it out.

Oh gotcha!

you can take away the diceCount1 from Dice.cs since you only have 1 value per dice.

                gui.text = "Dice Count : " + diceClone.GetComponent<Dice>().diceCount+diceClone1.GetComponent<Dice>().diceCount;

That would do the job in case the diceCount inside the Dice.cs actually works

Still I get the total of one dice but now it is 2 digits instead of 1 i.e. 05 instead of 5. I think I did not instansiate the dice properly but I have changed that to many different ways. If you cant tell I am pretty new at this.

using UnityEngine;
using System.Collections;

public class Dice : MonoBehaviour
{
        public int diceCount;
        public int diceCount1;
        public static Dice Instance;
        internal Vector3 initPos;
        void Start ()
        {
               GetComponent<Rigidbody>().solverIterationCount = 250;
                Instance = this;
                initPos = transform.position;
        }

        void OnEnable()
        {
            initPos = transform.position;
        }

        public int GetDiceCount ()
        {
                regularDiceCount ();
                return diceCount;
        }

        void regularDiceCount ()
        {
                if (Vector3.Dot (transform.forward, Vector3.up) > 0.6f)
                    diceCount = 5 + diceCount1;
                if (Vector3.Dot (-transform.forward, Vector3.up) > 0.6f)
                    diceCount = 2 + diceCount1;
                if (Vector3.Dot (transform.up, Vector3.up) > 0.6f)
                    diceCount = 3 + diceCount1;
                if (Vector3.Dot (-transform.up, Vector3.up) > 0.6f)
                    diceCount = 4 + diceCount1;
                if (Vector3.Dot (transform.right, Vector3.up) > 0.6f)
                    diceCount = 6 + diceCount1;
                if (Vector3.Dot (-transform.right, Vector3.up) > 0.6f)
                    diceCount = 1 + diceCount1;


//       Commented out these lines below
//                if (Vector3.Dot (transform.forward, Vector3.up) > 0.6f)
//                    diceCount1 = 5;
//                if (Vector3.Dot (-transform.forward, Vector3.up) > 0.6f)
//                    diceCount1 = 2;
//                if (Vector3.Dot (transform.up, Vector3.up) > 0.6f)
//                    diceCount1 = 3;
//                if (Vector3.Dot (-transform.up, Vector3.up) > 0.6f)
//                    diceCount1 = 4;
//                if (Vector3.Dot (transform.right, Vector3.up) > 0.6f)
//                    diceCount1 = 6;
//                if (Vector3.Dot (-transform.right, Vector3.up) > 0.6f)
//                    diceCount1 = 1;
        }
using UnityEngine;
using System.Collections;

public class DiceSwipeControl : MonoBehaviour
{
        // Static Instance of the Dice
        public static DiceSwipeControl Instance;
    #region Public Variables
        // Orignal Dice
        public GameObject die1;
        //dice resultant number..
        public int diceCount;
        public int diceCount1;
        //dice play view camera...
        public Camera dicePlayCam;
        //Can Throw Dice
        public bool isDiceThrowable = true;
        public GUIText gui;
        public Transform diceCarrom;
    #endregion

    #region Private Varibles
        private GameObject diceClone;
        private GameObject diceClone1;
        private Vector3 initPos;
        private float initXpose;
        private float timeRate;
        // To Save Camera Postion
        private Vector3 currentCampPos;
        Vector3 objectPos;
        internal float diceThrowInit;
    #endregion

        void Awake ()
        {
                Instance = this;

        }

        void Start ()
        {
                generateDice ();
        }

        void Update ()
        {
                if (isDiceThrowable)
                {
                    if (Input.GetMouseButtonDown (0)) 
                    {
                            initPos = Input.mousePosition;
                            initXpose = dicePlayCam.ScreenToViewportPoint (Input.mousePosition).x;
                    }
                    Vector3 currentPos = Input.mousePosition;
                    currentPos.z = 25f;
                    Vector3 newPos = dicePlayCam.ScreenToWorldPoint (new Vector3(currentPos.x,currentPos.y,Mathf.Clamp(currentPos.y/10,5,70)));
                    newPos.y = Mathf.Clamp(newPos.y, -114.5f, 100);
                    newPos = dicePlayCam.ScreenToWorldPoint (currentPos);
                    if (Input.GetMouseButtonUp (0)) 
                    {
                        initPos = dicePlayCam.ScreenToWorldPoint (initPos);
       
                        enableTheDice ();
                        addForce (newPos);
                        isDiceThrowable = false;
                               
                        StartCoroutine (getDiceCount ());
                    }
                }
        }

        void addForce (Vector3 lastPos)
        {
                diceClone.GetComponent<Rigidbody>().AddTorque(Vector3.Cross(lastPos, initPos) * 2000, ForceMode.Impulse);
                lastPos.y += 12;
                diceClone.GetComponent<Rigidbody>().AddForce (((lastPos - initPos).normalized) * (Vector3.Distance (lastPos, initPos)) * 40 * diceClone.GetComponent<Rigidbody>().mass);

                diceClone1.GetComponent<Rigidbody>().AddTorque(Vector3.Cross(lastPos, initPos) * 2000, ForceMode.Impulse);
                lastPos.y += 12;
                diceClone1.GetComponent<Rigidbody>().AddForce (((lastPos - initPos).normalized) * (Vector3.Distance (lastPos, initPos)) * 40 * diceClone1.GetComponent<Rigidbody>().mass);
    }
   
        void enableTheDice ()
        {       
                diceClone.transform.rotation = Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180));
                diceThrowInit = 2;

                diceClone1.transform.rotation = Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180));
                diceThrowInit = 2;
    }

        void generateDice ()
        {
                diceClone = Instantiate (die1, dicePlayCam.transform.position, Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180))) as GameObject;
                diceClone1 = Instantiate (die1, dicePlayCam.transform.position, Quaternion.Euler (Random.Range (0, 180), Random.Range (0, 180), Random.Range (0, 180))) as GameObject;
        }


    #region Coroutines

        IEnumerator getDiceCount ()
        {
                //Time.timeScale = 1.3f;   
                currentCampPos = dicePlayCam.transform.position;
                //wait fore dice to stop...
                yield return new WaitForSeconds (1.0f);
                while (diceClone.GetComponent<Rigidbody>().velocity.magnitude > 0.05f) {
                        yield return 0;

            }

                Time.timeScale = 0.2f;
                timeRate = 0.001f;
                float startTime = Time.time;
                Vector3 risePos = dicePlayCam.transform.position;
                Vector3 setPos = new Vector3 (diceCarrom.position.x, diceClone.transform.position.y + 25f, diceCarrom.position.z);
                float speed = 0.18f;
                float fracComplete = 0;

                while (Vector3.Distance(dicePlayCam.transform.position,setPos)>0.5f) 
                {
                        Vector3 center = (risePos + setPos) * 0.5f;
                        center -= new Vector3 (0, 2, -1);
                        Vector3 riseRelCenter = risePos - center;
                        Vector3 setRelCenter = setPos - center;
               
                        if (fracComplete > 0.85f && fracComplete < 1f) {
                                speed += Time.deltaTime * 0.3f;
                                Time.timeScale -= Time.deltaTime * 4f;
                        } 
                        dicePlayCam.transform.position = Vector3.Slerp (riseRelCenter, setRelCenter, fracComplete);
                        dicePlayCam.transform.position += center;
                        dicePlayCam.transform.LookAt (diceCarrom);
                        fracComplete = (Time.time - startTime) / speed;
                        yield return 0;
                }

                Time.timeScale = 1.0f;
                Dice.Instance.GetDiceCount ();
                gui.text = "Dice Count : " + Dice.Instance.GetDiceCount();
                yield return new WaitForSeconds (5f);

                Dice.Instance.diceCount = 0;
                Dice.Instance.GetDiceCount ();
                diceCount = Dice.Instance.GetDiceCount ();
               
//Changed this line per your suggestion
gui.text = "Dice Count : " + diceClone.GetComponent<Dice>().diceCount+diceClone1.GetComponent<Dice>().diceCount;
                diceClone.GetComponent<Rigidbody>().constraints = RigidbodyConstraints.None;
                Application.LoadLevel(0);
        }

    #endregion