Home > Enterprise >  unity Subtraction Issue - Beginner
unity Subtraction Issue - Beginner

Time:07-03

I have a really basic problem.

I want to display the score on my TextBox and add 1 when the "OK" button is clicked and subtract -5 when the "NO" button is clicked.

But now I have the problem that it seems to me that the subtraction doesn't really work. Only one instance is displayed on the TextBox, i.e. if I click OK 10x in a row, I have 10 Points. If I now click on NO, the text field immediately shows -5, but it should be subtracted. If I click OK again, then I'll have 11 points, which shouldn't be the case, it should be 6.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;


public class Clicked : MonoBehaviour
{
    public GameObject NO;
    public GameObject OK;
    public Text Points;
    public int score;
   
    public void OnClick()
    {
        Score(1);
        OK.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));
        double rnd = Random.Range(0, 11);
        if (rnd == 1)
        {
            NO.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));
            NO.SetActive(true);

        }
        else
        {
            NO.SetActive(false);
        }
    }
    public void Score(int points)
    {
        if(points == 5)
        {
            score = score - 5;
            Points.text = "Punkte : " score;
        }
        else if(points == 1)
        {
            score  ;
            Points.text = "Punkte : " score;
        }
       
    }
    public void OnClickNo()
    {
        Score(5);
        NO.SetActive(false);

    }

    void Start()
    {
        NO.SetActive(false);
      
        Points.text = "Punkte : "   score;
    }

}

EDIT I changed now everything and add a Listener at start. It seems now to work.

public class Clicked : MonoBehaviour
{
    public Text Points;
    public int score = 0;


    [SerializeField]
    private Button OK;

    [SerializeField]
    private Button NO;


    public Button SelectedButton
    {
        get; private set;
    }

    private void Start()
    {
        Points.text = "Punkte : "   score;
        NO.onClick.AddListener(() => SelectButton(NO));
        OK.onClick.AddListener(() => SelectButton(OK));
    }

    public void SelectButton(Button button)
    {
        SelectedButton = button;
        if(button == OK)
        {
            score  ;
            Points.text = "Punkte : "   score;
            OK.enabled = (SelectedButton == OK);
            OK.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));
            double rnd = Random.Range(0, 5);
            if (rnd == 1)
            {
                NO.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));

            }
        }
        else if(button == NO)
        {
            score -= 5;
            Points.text = "Punkte : "   score;
            NO.enabled = (SelectedButton == NO);
            NO.transform.position = new Vector2(Random.Range(90, Camera.main.pixelWidth - 90), Random.Range(90, Camera.main.pixelHeight - 90));

        }


    }
}

CodePudding user response:

You need to use a single script that will handle both button clicks. At the moment you have two scripts, each one with its own score variable. Place your script to the object (usually root object for both buttons is used for this). Then point both buttons onClick method to this script. It should do the trick.

CodePudding user response:

The best possible solution is adding an empty parent (or any object for that matter). The simplest way would be a parent for simplicity's sake. Now the parent would be the only one counting the points and the buttons would only call functions to change the same score:

Adding 2 buttons (or more) to the same script:

public Button YesButton;
public Button NoButton;

Capturing their clicks and counting points all at once:

YesButton.onClick.AddListener(() => Score(1));
NoButton.onClick.AddListener(() => Score(5));

This would pretty much go in the Start() function. None of it needs to repeat. After that the code counts points CORRECTLY

FOR ADDITIONAL COMMANDS UPON CLICKING

CHANGE THE SCORE FUNCTION TO DO THE REST OR ADD NEW FUNCTIONS WHICH CALL SCORE AND DISABLE THE BUTTONS

  • Related