I'm new to Unity and C#. I'm making a clicker game where you write books for money. In order to calculate how much each book should be worth in dollars I have a script called "Money Calculations" where I calculate how much money a book with certain creativity and number of pages should be worth, then I send the number of pages and the book's creativity as variables to my other script "money display". After running simple mathematical functions in Money Calculations I come out with a whole number, which is what I want.
This is the script which generates the dollar amount and calls a method to another script below to take the number and display it. This bit is the "Money Calculations":
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Money Calculations : MonoBehaviour
{
public static int cashAmount;
public static float pages;
public static float creativity;
public void displayMoney()
{
//money formula
pages = Mathf.RoundToInt(CriticsP.numOfPages);
creativity = Mathf.RoundToInt(CriticsC.creativity);
// -Critics C and P are other scripts
// -pages and creativity start as float values
cashAmount = (int)(pages / 3) + (int)(creativity * 4);
//cashAmount is dollar value to be sent to Money Display
MoneyDisplay.add();
}
}
And here I have the display script, "Money Display"
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoneyDisplay : MonoBehaviour
{
public static int cashOnHand;
public Text cashMoney;
void Update()
{
cashMoney.text = cashOnHand.ToString();
}
public static void add()
{
// **
cashOnHand += (int)MoneyCalculations.cashAmount;
}
}
The problem I'm running into is where I have the asterisks. The original numbers for pages and creativity were floats, I was having trouble doing calculations on them as ints. So I figured I could convert them to ints before I displayed the final number, cashOnHand, which is an int. Even though I've said several times for C# to convert pages and creativity to ints, I think I must be doing it wrong. When I say cashOnHand = (int)MoneyCalculations.cashAmount
the number the game displays is a whole number, the same number exactly as the previous script generates. But for some reason when I tell the computer cashOnHand * += * (int)MoneyCalculations.cashAmount
(asterisks for emphasis) it prints a wild number, like 7880 instead of 325 like I need. I suspect it's reverting to it's base float value and changing from the integer I'm looking for, I don't know why the number returns to a float when I specifically perform a += function, and I'm out of ideas to fix it.
I've tried doing several conversions from float to int in order to get my number, but I was unable to maintain page number and creativity as ints throughout the development process and had to use floats for lack of expertise. I'm afraid it could be too late now, as the floats are ingrained in the code in several places and I'm worried a simple conversion may not be enough, but I don't know. If my case is hopeless, could you refer me to a resource on performing math functions on integers without having to have them as floats?