So, in my game I want it to calculate the total of 2 variables of two different game objects. What I want it to do is when I press a button, it gets the script of the 2 selected game objects and gets the variable "kalori", then it sums them and gives a total. But in my script it just gives "0" as the total everytime. I'm pretty new to unity so sorry if this is a stupid question. Anyway here is the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CalculateAnswer : MonoBehaviour
{
    public GameObject correct;
    Text correctText;
    Text wrongText;
    public GameObject wrong;
    public float max;
    public GameObject[] selectedCalories;
    public Selection script;
    
    // Start is called before the first frame update
    void Start()
    {
       correct = GameObject.FindWithTag("Correct");
       wrong = GameObject.FindWithTag("Wrong");
       correctText = correct.GetComponent<Text>();
       wrongText = wrong.GetComponent<Text>();
    }
    // Update is called once per frame
    void Update()
    {
        
    }
    void OnMouseDown(){
        float total = 0;
        selectedCalories = GameObject.FindGameObjectsWithTag("selected");
        calculateTotal(total);
        if(total > 0)
        {
            Debug.Log(total);
            if(total < max){
                correctText.enabled = true;
                wrongText.enabled = false;
                total = 0;
            }
            else if(total > max){
                wrongText.enabled = true;
                correctText.enabled = false;
           
                total = 0;
            }
        }
    }
    void calculateTotal(float total){
        foreach (GameObject selected in selectedCalories)
        {
            script = selected.GetComponent<Selection>();
            total = total + script.kalori;
        }
    }
If you need the script for the selection of the game objects:
   SpriteRenderer renderer;
   public float kalori;
   public Vector3 currentScale = new Vector3(2, 2, 1);
   public Vector3 changedScale = new Vector3(3, 3, 1);
   public GameObject currentlySelected;
   public int numberOfSelected;
    void Start()
    {
        renderer = gameObject.GetComponent<SpriteRenderer>();
    }
    void Update()
    {
        currentlySelected = GameObject.FindWithTag("selected");
        numberOfSelected =  GameObject.FindGameObjectsWithTag("selected").Length;
    }
    void OnMouseOver() {
        transform.localScale = changedScale;
    }
    void OnMouseExit() {
        transform.localScale = currentScale;
    }
    void OnMouseDown() {
        if(currentlySelected == null || numberOfSelected < 2){
            if(renderer.color == Color.white){
                renderer.color = Color.green;
                gameObject.tag = "selected";
            }
        }
        else if(renderer.color == Color.green){
            renderer.color = Color.white;
            gameObject.tag = "unselected";
        }
        
    }
I tried making it get both of the game objects with the tag "selected" and sum their "kalori" variables in their "Selection" script. Instead when it sums the variables up I get 0.
Thanks in advance.
