D
D
dark_spectator2018-12-08 22:52:41
C++ / C#
dark_spectator, 2018-12-08 22:52:41

Math operations with ulong type?

Hello, there is a number with type ulong, let's say 100057, you need to divide it by 5, while the number should be an integer, i.e. without a trace. How can I do that?
In general, the problem is very global.
There is a code:

using UnityEngine;
using UnityEngine.UI;
using System;
using Newtonsoft.Json;

public class TasksHelper : MonoBehaviour
{



    public GameHelper gameHelper; // Экземпляр класса GameHelper

    public Text task_progress_text; // Текстовое поле с прогрессом игрока
    public Text task_target_text; // Текстовое поле с целью задачи 
    public Text task_type_text; // Текстовое поле с описанием задачи
    public Text task_reward_text; // Текстовое поле с наградой

    private SaveCurrentTask saveCurrentTask = new SaveCurrentTask();



    public ulong reward;
    public int clicks;
    public int clicksTarget;


    public ulong GetClickTaskReward(int target)
    {
        if (target == 1000)
        {
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            Debug.Log("[TASK HELPER] - CLICK REWARD -> " + gameHelper.dollars / 5);
            return gameHelper.dollars / 5;
        }
        else if (target == 1500)
        {
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            Debug.Log("[TASK HELPER] - CLICK REWARD -> " + gameHelper.dollars / 4);
            return gameHelper.dollars / 4;
        }
        else if (target == 2000)
        {
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            Debug.Log("[TASK HELPER] - CLICK REWARD -> " + gameHelper.dollars / 3);
            return gameHelper.dollars / 3;
        }
        else if (target == 5000)
        {
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            Debug.Log("[TASK HELPER] - CLICK REWARD -> " + gameHelper.dollars / 2);
            return gameHelper.dollars / 2;
        }
        else if (target == 10000)
        {
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            Debug.Log("[TASK HELPER] - CLICK REWARD -> " + gameHelper.dollars);
            return gameHelper.dollars;
        }
        else if (target == 15000)
        {
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            Debug.Log("[TASK HELPER] - CLICK REWARD -> " + gameHelper.dollars * 2);
            return gameHelper.dollars + gameHelper.dollars * 2;
        }
        else
        {
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            return gameHelper.dollars + gameHelper.dollars * 5;
        }
    }


    
    public void GetBalanceTask()
    {
        int key = UnityEngine.Random.Range(1, 100);
    }




    public int CountClickGetter(int key)
    {
        if (key > 0 & key < 20)
        {
            return 1000;
        }
        else if (key > 20 & key < 40)
        {
            return 1500;
        }
        else if (key > 40 & key < 60)
        {
            return 2000;
        }
        else if (key > 60 & key < 80)
        {
            return 5000;
        }
        else if (key > 80 & key < 100)
        {
            return 10000;
        }
        else
        {
            return 15000;
        }
    }
    public void GetClickTask()
    {
        if (!PlayerPrefs.HasKey("Task"))
        {
            int key = UnityEngine.Random.Range(1, 100);
            clicksTarget = CountClickGetter(key);
            task_target_text.text = clicksTarget.ToString();
            task_progress_text.text = clicks.ToString();
            task_type_text.text = "TAP" + clicksTarget.ToString() + "TIMES";
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            reward = GetClickTaskReward(key);
            if(reward == 0) { reward = 5000; }
            task_reward_text.text = gameHelper.FormatMoney(reward);
            PlayerPrefs.SetString("ClickTask", "active");
            PlayerPrefs.SetString("Task", "a");
        }


    }
    

    private void Start()
    {
        if (!PlayerPrefs.HasKey("SaveTask"))
        {
            GetClickTask();
        }
        else
        {
            saveCurrentTask = JsonConvert.DeserializeObject<SaveCurrentTask>(PlayerPrefs.GetString("SaveTask"));
            clicks = saveCurrentTask.clicks;
            reward = saveCurrentTask.reward;
            clicksTarget = saveCurrentTask.clicksTarget;
        }
    }

    private void Update()
    {
        if (PlayerPrefs.HasKey("Task"))
        {
            if (PlayerPrefs.HasKey("ClickTask"))
            {

                task_target_text.text = clicksTarget.ToString();
                task_progress_text.text = clicks.ToString();
                gameHelper = GameObject.FindObjectOfType<GameHelper>();
                task_reward_text.text = gameHelper.FormatMoney(reward);
                task_type_text.text = "TAP " + clicksTarget.ToString() + " TIMES";
                if (clicksTarget <= clicks)
                {
                    gameHelper.dollars += reward;
                    reward = 0;
                    clicks = 0;
                    clicksTarget = 0;
                    PlayerPrefs.DeleteKey("Task");
                    PlayerPrefs.DeleteKey("ClickTask");
                    GetClickTask();
                }

                saveCurrentTask.clicks = clicks;
                saveCurrentTask.clicksTarget = clicksTarget;
                saveCurrentTask.reward = reward;

                string saveTaskJson = JsonConvert.SerializeObject(saveCurrentTask);
                PlayerPrefs.SetString("SaveTask", saveTaskJson);
            }
        }
    }

}

[Serializable]
class SaveCurrentTask
{
    public int clicks;
    public int clicksTarget;

    public ulong reward;
}

Here the value of the reward variable with a balance of 0, = 5000. But on the next round, it already gives ~ 30000, there seem to be no errors in the code. Who can explain why this is happening?
There are no entries in the console by type [TASK HELPER] - CLICK REWARD -> 5000.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dark_spectator, 2018-12-08
@dark_spectator

The problem lay in the change that was transferred to GetClickTaskReward()
It should be transferred clicksTarget insteadkey

public void GetClickTask()
    {
        if (!PlayerPrefs.HasKey("Task"))
        {
            int key = UnityEngine.Random.Range(1, 100);
            clicksTarget = CountClickGetter(key);
            task_target_text.text = clicksTarget.ToString();
            task_progress_text.text = clicks.ToString();
            task_type_text.text = "TAP" + clicksTarget.ToString() + "TIMES";
            gameHelper = GameObject.FindObjectOfType<GameHelper>();
            reward = GetClickTaskReward(key);
            if(reward == 0) { reward = 5000; }
            task_reward_text.text = gameHelper.FormatMoney(reward);
            PlayerPrefs.SetString("ClickTask", "active");
            PlayerPrefs.SetString("Task", "a");
        }


    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question