- Home /
The question is answered, right answer was accepted
Unity Calculation fails
I have the following problem that I am despairing of, and that is I have a simple calculation which for whatever reason does not give the correct result. My script looks like this:
using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.Advertisements;
public class OfflineIncome : MonoBehaviour, IUnityAdsListener
{
public Items OfflineItem;
public Clicker clicker;
public Save save;
public Rps rps;
public int MaxOfflineTime = 7200;
public float CurrentOfflineTime;
public float OfflineEarned;
public float AwayFor;
public float RewardedFor;
public Text OfflineEarnedText;
public Text AwayForText;
public Button TakeButton;
public Button DoubleButton;
public GameObject OfflinePanel;
public GameObject AdStatusPanel;
public Button AcceptAdStatus;
public Text AdStatus;
public Text AdStatusText;
DateTime currentTime;
DateTime endTime;
public double seconds;
public float days;
public float hours;
public float minutes;
public float secs;
#if UNITY_IOS
private string gameId = "4124812";
#elif UNITY_ANDROID
private string gameId = "4124813";
#endif
bool testMode = true;
public string myPlacementId = "rewardedVideo";
private void Start()
{
Advertisement.Initialize(gameId, testMode);
Advertisement.AddListener(this);
DoubleButton.GetComponent<Button>();
DoubleButton.interactable = Advertisement.IsReady(myPlacementId);
if (DoubleButton) DoubleButton.onClick.AddListener(ShowRewardedVideo);
}
void ShowRewardedVideo()
{
Advertisement.Show(myPlacementId);
}
public void OnUnityAdsReady(string placementId)
{
if (placementId == myPlacementId)
{
DoubleButton.interactable = true;
}
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
if (showResult == ShowResult.Finished)
{
OfflineEarned *= 2;
clicker.Radiation += OfflineEarned;
AdStatusPanel.SetActive(true);
AdStatus.text = "Ad finished successfully";
AdStatusText.text = "You earned: " + OfflineEarned.ToString("F2") + " Radiation";
}
else if (showResult == ShowResult.Skipped)
{
//Kein Reward
}
else if (showResult == ShowResult.Failed)
{
AdStatusPanel.SetActive(true);
AdStatus.text = "Ad failed";
AdStatusText.text = "The ad did not finish due to an error!";
}
}
public void OnUnityAdsDidError(string message)
{
}
public void OnUnityAdsDidStart(string placementId)
{
}
public void Accept()
{
AdStatusPanel.SetActive(false);
}
public void Take()
{
clicker.Radiation += OfflineEarned;
OfflinePanel.SetActive(false);
}
public void Started()
{
//rps.CalculateRadiationPerSecond();
OfflinePanel.SetActive(true);
currentTime = System.DateTime.Now;
long temp = Convert.ToInt64(PlayerPrefs.GetString("sysString"));
endTime = DateTime.FromBinary(temp);
TimeSpan difference = currentTime.Subtract(endTime);
//string TimeText = string.Format("{0:D2}:{1:D2}:{2:D2}:{3:D2}", difference.Days, difference.Hours, difference.Minutes, difference.Seconds);
seconds = difference.TotalSeconds;
days = Convert.ToSingle(difference.Days);
hours = Convert.ToSingle(difference.Hours);
minutes = Convert.ToSingle(difference.Minutes);
secs = Convert.ToSingle(difference.Seconds);
if (seconds >= MaxOfflineTime)
{
//For testing
Debug.Log("Offlinetime > MaxOfflineTime");
Debug.Log("MaxOfflineTime: " + MaxOfflineTime);
Debug.Log("Rp/s: " + rps.RadiationPerSecond);
Debug.Log("OfflineIncome: " + OfflineItem.OfflineIncome);
Debug.Log("OfflineEarnedBefore: " + OfflineEarned);
OfflineEarned = (MaxOfflineTime * OfflineItem.OfflineMultiplier) * rps.RadiationPerSecond;
Debug.Log("OfflineEarnedAfter: " + OfflineEarned);
}
else if (seconds < MaxOfflineTime)
{
//For testing
Debug.Log("OfflineTime < MaxOfflineTime");
Debug.Log("seconds: " + seconds);
Debug.Log("Rp/s: " + rps.RadiationPerSecond);
Debug.Log("OfflineIncome: " + OfflineItem.OfflineIncome);
OfflineEarned = (float)seconds * (rps.RadiationPerSecond * OfflineItem.OfflineIncome);
}
if(days > 0)
{
AwayForText.text = days + "d " + hours + "h " + minutes + "m " + secs + "s";
}
else if( hours > 0)
{
AwayForText.text = hours + "h " + minutes + "m " + secs + "s";
}
else if (minutes > 0)
{
AwayForText.text = minutes + "m " + secs + "s";
}
else if (secs> 0)
{
AwayForText.text = secs + "s";
}
OfflineEarnedText.text = OfflineEarned.ToString("F1") + " Radiation";
//AwayForText.text = TimeText;
}
public void Double()
{
OfflinePanel.SetActive(false);
}
private void OnApplicationQuit()
{
PlayerPrefs.SetString("sysString", System.DateTime.Now.ToBinary().ToString());
Debug.Log("Saving this time: " + System.DateTime.Now);
}
private void OnApplicationPause(bool pause)
{
if (pause)
{
PlayerPrefs.SetString("sysString",
System.DateTime.Now.ToBinary().ToString());
Debug.Log("Saving this time: " + System.DateTime.Now);
}
}
}
The error is in the calculation of offlineincome once seconds > Maxofflinetime. The calculation should be as follows according to the console output: OfflineEarned = (MaxOfflineTime(7200) OfflineItem.OfflineMultiplier(1)) rps.RadiationPerSecond(1); However, the result is always 18000, which makes no sense in my eyes. I will also add a screenshot of the console.
Answer by Bunny83 · Oct 21, 2021 at 03:18 PM
Are you sure that your two formulas are right? If the time is below the max time you actually multiply by "OfflineIncome". However when you are above the max time you multiply by "OfflineMultiplier" instead.
This seems a bit weird. If you just want to have a max time, why don't you just clamp the seconds?
You haven't printed out the value of "OfflineMultiplier" only "OfflineIncome", so I guess you used the wrong variable?
As I said it would be much easier to use the same calculation and just clamp the seconds
if (seconds > MaxOfflineTime)
{
seconds = MaxOfflineTime;
}
// do your calculation here using seconds as usual.
Of course you can also clamp the seconds like this as well
seconds = Mathf.Min(difference.TotalSeconds, MaxOfflineTime);
or
seconds = Mathf.Clamp(difference.TotalSeconds, 0, MaxOfflineTime);
Follow this Question
Related Questions
GUI error calculating 2 Answers
Math Genius NEEDED! Algorithm problem with parabola 1 Answer
Hit box based off of a parabola 1 Answer
Mathf.Lerp like User Define Function 2 Answers
Please help me to find a better way to calculate prices. 1 Answer