- Home /
Please help me to find a better way to calculate prices.
I have a panel with items, a list with added items and a text where the total price and mass is shown. The price for every object is written. I related the objects with Gameobjects elements in Unity and when one Gameobject is activated then another object (text with price) will be shown. However I can't write 1000 combinations this way if I have many objects. Is there a more clever solution? Here is my code.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI; // Wichtig! Muss vorhanden sein!
public class Calculations : MonoBehaviour
{
public GameObject TextRumpfPrice, TextCabinPrice, TextMotorPrice;
private int rumpfprice = 10000;
private int cabinprice = 4000;
private int motorprice = 15000;
private int rumpfweight = 5000;
private int cabinweight = 2000;
private int motorweight = 1000;
private int totalprice1, totalprice2, totalprice3, totalprice4, zerototal;
private int totalweight1, totalweight2, totalweight3, totalweight4, zeroweight;
public Text TextTotalPrice1, TextTotalPrice2, TextTotalPrice3, TextTotalPrice4, TextZeroPrice;
public Text SSTextTotalPrice1, SSTextTotalPrice2, SSTextTotalPrice3, SSTextTotalPrice4, SSTextZeroPrice;
public Text SSTextTotalWeight1, SSTextTotalWeight2, SSTextTotalWeight3, SSTextTotalWeight4, SSTextZeroWeight;
public Text TextTotalWeight1, TextTotalWeight2, TextTotalWeight3, TextTotalWeight4, TextZeroWeight;
public Text TextCabinPrice1, TextRumpfPrice1, TextMotorPrice1;
public Text TextCabinWeight1, TextRumpfWeight1, TextMotorWeight1;
void Start()
{
}
void Update()
{
if(TextRumpfPrice.activeInHierarchy && !TextCabinPrice.activeInHierarchy && !TextMotorPrice.activeInHierarchy)
{
TextRumpfPrice1.text.ToString();
TextRumpfPrice1.text = rumpfprice.ToString("C2");
TextRumpfWeight1.text.ToString();
TextRumpfWeight1.text = rumpfweight.ToString();
}
if(TextCabinPrice.activeInHierarchy && !TextRumpfPrice.activeInHierarchy && !TextMotorPrice.activeInHierarchy)
{
TextCabinPrice1.text.ToString();
TextCabinPrice1.text = cabinprice.ToString("C2");
TextCabinWeight1.text.ToString();
TextCabinWeight1.text = cabinweight.ToString();
}
if(TextMotorPrice.activeInHierarchy && !TextRumpfPrice.activeInHierarchy && !TextCabinPrice.activeInHierarchy)
{
TextMotorPrice1.text.ToString();
TextMotorPrice1.text = motorprice.ToString("C2");
TextMotorWeight1.text.ToString();
TextMotorWeight1.text = motorweight.ToString();
}
if(TextRumpfPrice.activeInHierarchy && TextCabinPrice.activeInHierarchy && !TextMotorPrice.activeInHierarchy)
{
totalprice1 = rumpfprice + cabinprice;
TextTotalPrice1.text.ToString();
TextTotalPrice1.text = totalprice1.ToString("C2");
totalweight1 = rumpfweight + cabinweight;
TextTotalWeight1.text.ToString();
TextTotalWeight1.text = totalweight1.ToString();
}
if(TextRumpfPrice.activeInHierarchy && TextCabinPrice.activeInHierarchy && TextMotorPrice.activeInHierarchy)
{
totalprice2 = rumpfprice + cabinprice + motorprice;
TextTotalPrice2.text.ToString();
TextTotalPrice2.text = totalprice2.ToString("C2");
totalweight2 = rumpfweight + cabinweight + motorweight;
TextTotalWeight2.text.ToString();
TextTotalWeight2.text = totalweight2.ToString();
}
if(TextRumpfPrice.activeInHierarchy && TextMotorPrice.activeInHierarchy && !TextCabinPrice.activeInHierarchy)
{
totalprice3 = rumpfprice + motorprice;
TextTotalPrice3.text.ToString();
TextTotalPrice3.text = totalprice3.ToString("C2");
totalweight3 = rumpfweight + motorweight;
TextTotalWeight3.text.ToString();
TextTotalWeight3.text = totalweight3.ToString();
}
if(TextCabinPrice.activeInHierarchy && TextMotorPrice.activeInHierarchy && !TextRumpfPrice.activeInHierarchy)
{
totalprice4 = cabinprice + motorprice;
TextTotalPrice4.text.ToString();
TextTotalPrice4.text = totalprice4.ToString("C2");
totalweight4 = cabinweight + motorweight;
TextTotalWeight4.text.ToString();
TextTotalWeight4.text = totalweight4.ToString();
}
if(!TextCabinPrice.activeInHierarchy && !TextMotorPrice.activeInHierarchy && !TextRumpfPrice.activeInHierarchy)
{
totalprice2 = rumpfprice + cabinprice + motorprice;
zerototal = totalprice2 - (rumpfprice + cabinprice + motorprice);
TextZeroPrice.text.ToString();
TextZeroPrice.text = zerototal.ToString("C2");
totalweight2 = rumpfweight + cabinweight + motorweight;
zeroweight = totalweight2 - (rumpfweight + cabinweight + motorweight);
TextZeroWeight.text.ToString();
TextZeroWeight.text = zeroweight.ToString();
}
}
}
Answer by JDelekto · Apr 27, 2019 at 11:46 AM
I would start by creating a script you can attach to all of the items to which you want to assign a weight and a price. For example:
using UnityEngine;
public class Item : MonoBehaviour
{
public float weight;
public float price;
}
Select all of your objects in your hierarchy that can be "Items" with weight and price, then drag that script onto them. Set the properties for the price and weight for each of the individual items on the script properties.
Next, wherever you wish to calculate these totals, just get all the game objects of that type, then calculate the totals if they are active:
float totalPrice = 0.0f;
float totalWeight = 0.0f;
Item[] itemsForSale = GameObject.FindObjectsOfType<Item>();
for (int index = 0, length = itemsForSale.Length; index < length; index++)
{
if (itemsForSale[index].gameObject.activeInHierarchy)
{
totalPrice += itemsForSale[index].price;
totalWeight += itemsForSale[index].weight;
}
}
You can add other attributes to your "Item" object, such as a display name, color, icon, etc.
Thank you so much! I will give a try and let you know if it works :)
Thank you so much! It works perfect but.. If I deactivate all gameobjects - the price and weight are being reduced but the last price property always stays in the text field even if all gameobjects are deactivated. The property of the last deactivated gameobject remains in the total cost field and doesn't show: "0" as it's supposed to be.
Could you please tell me why it's like that?
I attached the picture, one can see that there all game objects are deactivated there but still the last price property is left in the Textfield and shows: 20000 of total price and 2000 of weight.
Another picture shows the properties of one of the game elements...
$$anonymous$$any thanks!
Regards $$anonymous$$s
![void Update()
{
float totalPrice = 0.0f;
float totalWeight = 0.0f;
Property[] itemsForSale = GameObject.FindObjectsOfType<Property>();
for (int index = 0, length = itemsForSale.Length; index < length; index ++)
{
if (itemsForSale[index].gameObject.activeInHierarchy)
{
totalPrice += itemsForSale[index].price;
totalWeight += itemsForSale[index].weight;
Debug.Log(totalPrice);
Debug.Log(totalWeight);
string PriceResult = totalPrice.ToString();
string WeightResult = totalWeight.ToString();
PriceResult = PriceResultText.text;
PriceResultText.text = totalPrice.ToString();
WeightResult = WeightResultText.text;
WeightResultText.text = totalWeight.ToString();
}
}
}][1]
You are setting the text for the total price and weight inside of your if() statement. If no items are active, it will not set the text correctly with the 0 value.
$$anonymous$$ove the code that sets the text for the total price and weight outside of the if() and for() statements, right before Update() returns. This should always show the correct weight when either all or none of the items are active.