- Home /
summing up items price in runtime
Hello
I have a script on each item with a float variable, to which I manually enter a price for the item (it is a float because it has cents in it = e.g 2.30 $$). Upon entering the item to the cart, I want the script that is on the cart, to sum up the price numbers each time an item is entering the collider.
So I need the cart script to reference the item script, right? So I reference the float variable and place a sum method on the cart script that actually update the new sum each time the item enters the cart.
So I hold a new float variable "totalPrice" that will hold the sum of all the items price, right?
Can that be done?
Answer by look001 · Jul 26, 2017 at 07:56 PM
The script that is on the collider will add the prices together. The code will look something like this (snipped):
void totalPrice = 0;
void OnCollisionEnter(Collision collision)
{
float price = collision.gameObject.GetComponent<YourItemScriptName> ().price; // the cost variable has to be public!
totalPrice += price;
Debug.Log (totalPrice);
}