Integer Varible Not Changing On Certain Lines

Lines 80, 81 and 88 have no effect and do not change the variables as intended. I am able to change the variables on the if statement on line 104 and do not understand why I cannot on the other lines. The if statements definitely are called as it displays the debug.log notifications but the variables do not change.

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

public class SlotMachine : MonoBehaviour
{

   
    public bool isSpinning;

    public bool slotHandlePressed;
    public bool increaseBetPressed;

    public int rowOneNum;
    public int rowTwoNum;
    public int rowThreeNum;

  
   

    Text rowOneText;
    Text rowTwoText;
    Text rowThreeText;

    Text creditText;
    Text betAmountText;

    int creditAmount = 1000;
    int betAmount;


    void Start()
    {
        creditText = GameObject.Find("Credit").GetComponent<UnityEngine.UI.Text>();
        betAmountText = GameObject.Find("BetAmount").GetComponent<UnityEngine.UI.Text>();


        rowOneText = GameObject.Find("Row1").GetComponent<UnityEngine.UI.Text>();
        rowTwoText = GameObject.Find("Row2").GetComponent<UnityEngine.UI.Text>();
        rowThreeText = GameObject.Find("Row3").GetComponent<UnityEngine.UI.Text>();
    }



    void Update()
    {
        if (isSpinning == true)
        {
            rowOneText.text = Random.Range(1, 9).ToString();
            rowTwoText.text = Random.Range(1, 9).ToString();
            rowThreeText.text = Random.Range(1, 9).ToString();

            isSpinning = false;

            CheckForWin();


            Debug.Log("GIVEN RANDOM NUMBER");
        }

        if (Input.GetKeyDown("space"))
        {
            Debug.Log("Credit = " + creditAmount);
            Debug.Log("Bet = " + betAmount);
        }

    }

    void CheckForWin()
    {
        Debug.Log("CHECKED NUMBERS");
        int.TryParse(rowOneText.text, out rowOneNum);
        int.TryParse(rowTwoText.text, out rowTwoNum);
        int.TryParse(rowThreeText.text, out rowThreeNum);

        if (rowOneNum == rowTwoNum || rowTwoNum == rowThreeNum || rowOneNum == rowThreeNum)
        {
            creditAmount = betAmount * 2 + creditAmount;
            betAmount = 0;

            Debug.Log("WON BET");
        }

        else
        {
            betAmount = 0;
            Debug.Log("LOST BET");
        }
    }


    void OnMouseDown()
    {

        if (slotHandlePressed && isSpinning == false)
        {
            isSpinning = true;

            Debug.Log("CLICKED HANDLE");
        }

        else if (increaseBetPressed && isSpinning == false && creditAmount > 0)
        {
            creditAmount = creditAmount - 10;
            betAmount = betAmount + 10;

            Debug.Log("INCREASED BET");
        }

    }

Try logging their values both right before and right after you change them (inside the same “if” blocks). I suspect you’ll find the assignments work fine and something else is going wrong.

1 Like

Have just tried this and the variables do not change I am very confused.

Please post the logging code and the log output from your test.

6359688--707583--console.jpg

6359694--707586--console2.jpg

So before you set the values, creditAmount is 1000 and betAmount is 0.

You set creditAmount = betAmount * 2 + creditAmount;
That’s 0 * 2 + 1000 = 1000.

You set betAmount = 0.

Afterwards, creditAmount is 1000 and betAmount is 0–exactly the values you just set them to. I see no reason to suspect those assignments are not working. What were you expecting to happen?

The value of betAmount is set under the OnMouseDown function when the increasedBetPressed collider is clicked, the value of betAmount is then changed by 10 and shows that it has changed in the log

But your logs clearly show that it has a value of 0 right before you try to add twice the bet to the current credit. That isn’t the fault of the assignment statements you pointed out (which haven’t yet run at that point). There must be a problem somewhere before that, which caused betAmount to have a different value than you expected by the time those assignments ran.

Perhaps some other code reset the variable between these two events. Or perhaps you have two instances of the SlotMachine class (possibly one of them is on a prefab rather than in your scene), each with their own copies of the variables, and you increased the bet on one copy but then checked for a win on the other.

Regardless, the variable assignments on lines 77-89 do not appear to be your problem.

Hi, thank you so much for taking the time to help with my problem, I used the same script for multiple objects and didn’t know that each object would have its own value for the variable, I changed the integers to static integers and now it works fine. I definitely wouldn’t have realized this without your help, thank you again so much