0

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.

CodeUser
  • 1
  • 3
  • In your mousedown, you say if total>0 and then total < max set total to 0, or if its > max then set it to 0 .. so basically unless its max, its going to be 0 – BugFinder Aug 30 '23 at 10:14
  • 1
    @BugFinder But probably this branch is never entered anyways, because `total` will never be `> 0` because it's initialized to `0` and never changed ... – derpirscher Aug 30 '23 at 10:17
  • @Luuk Why would you need to pass an array of objects to a function that expects a double as parameter? `selectedCalories` is a class member, so `calculateTotal` already has access to it, no need to pass it – derpirscher Aug 30 '23 at 10:19
  • @derpirscher I did end my comment with "Please debug your code and check if that's true." – Luuk Aug 30 '23 at 10:22
  • 1
    `calculateTotal(total);` will not in any way change the value of the `var total = 0f;` you created in `OnMouseDown` ... if that was the intention you either want to return a new value from `calculateTotal` or pass it in as a [`ref float`](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/ref#passing-an-argument-by-reference) – derHugo Aug 30 '23 at 10:30
  • I would btw also not go by tags for the selection but rather add/remove them to/from a collection (`HashSet`) to avoid a lot of redundant work .. especially every frame in `Update` .. – derHugo Aug 30 '23 at 10:33
  • 1
    @Luuk yes and you started it with "without reading your full code" Maybe you should read the question's code first before giving (wrong) advice – derpirscher Aug 30 '23 at 10:33
  • @derHugo Thanks, that was the answer I was looking for. I apologize for my mistake, I didn't know of ref floats before you told me since I'm pretty new to C#. My code works as I need now. Also, I will try what you said about collections, I have not heard of them before. – CodeUser Aug 31 '23 at 09:17
  • a `HashSet` is basically the same as a `List`, but the elements have no specific order and are unique (-> no duplicates) – derHugo Aug 31 '23 at 09:20

0 Answers0