In void bool dont changing
the boolean CPU1 should unactive the button CPU1btn, but when i click on this button CPU1 dont changes, pls help
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class ShopScene : MonoBehaviour {
public Image progressbar;
private int perfomance;
private int money;
private bool CPU1 = false;
private bool CPU2 = false;
private bool CPU3 = false;
private bool CPU4 = false;
private bool CPU5 = false;
private bool CPU6 = false;
private bool GPU1 = false;
private bool GPU2 = false;
private bool GPU3 = false;
private bool GPU4 = false;
private bool GPU5 = false;
private bool GPU6 = false;
public Button CPU1btn;
public Button CPU2btn;
public Button CPU3btn;
public Button CPU4btn;
public Button CPU5btn;
public Button CPU6btn;
public Button GPU1btn;
public Button GPU2btn;
public Button GPU3btn;
public Button GPU4btn;
public Button GPU5btn;
public Button GPU6btn;
public void falsekek(bool a, int b, int c)
{
if (c == 0) {
if (PlayerPrefs.HasKey("CPU" + b))
{
a = System.Convert.ToBoolean(PlayerPrefs.GetInt("CPU" + b));
}
}
else
{
a = System.Convert.ToBoolean(PlayerPrefs.GetInt("GPU" + b));
}
}
void Start () {
falsekek(GPU1, 1, 1);
falsekek(GPU2, 2, 1);
falsekek(GPU3, 3, 1);
falsekek(GPU4, 4, 1);
falsekek(GPU5, 5, 1);
falsekek(GPU6, 6, 1);
falsekek(CPU1, 1, 0);
falsekek(CPU2, 2, 0);
falsekek(CPU3, 3, 0);
falsekek(CPU4, 4, 0);
falsekek(CPU5, 5, 0);
falsekek(CPU6, 6, 0);
perfomance = PlayerPrefs.GetInt("perfomance");
money = PlayerPrefs.GetInt("money");
}
public void checkbtn(Button a,bool b)
{
if (b == true)
{
a.interactable = false;
}
}
void Update () {
checkbtn(CPU1btn, CPU1);
checkbtn(CPU2btn, CPU2);
checkbtn(CPU3btn, CPU3);
checkbtn(CPU4btn, CPU4);
checkbtn(CPU5btn, CPU5);
checkbtn(CPU6btn, CPU6);
checkbtn(GPU1btn, GPU1);
checkbtn(GPU2btn, GPU2);
checkbtn(GPU3btn, GPU3);
checkbtn(GPU4btn, GPU4);
checkbtn(GPU5btn, GPU5);
checkbtn(GPU6btn, GPU6);
progressbar.fillAmount = perfomance / 100;
}
public void Buy(int a, bool b,int c, int d)
{
if (money >= a)
{
money -= a;
b = true;
perfomance += 7;
PlayerPrefs.SetInt("money", money);
if (c == 1) {
PlayerPrefs.SetInt("GPU"+d,System.Convert.ToInt32(b));
}
if (c == 0)
{
PlayerPrefs.SetInt("CPU" + d, System.Convert.ToInt32(b));
}
}
else
{
if (PlayerPrefs.GetString("language") == "eng")
{
AndroidNativeFunctions.ShowToast("You dont have money for this!");
}
else
{
AndroidNativeFunctions.ShowToast("У вас нет денег для этого!");
}
}
}
//btn gpu
public void GPUbtn1()
{
Buy(100,GPU1,1,1);
}
public void GPUbtn2()
{
Buy(140, GPU2, 1, 2);
}
public void GPUbtn3()
{
Buy(190, GPU3, 1, 3);
}
public void GPUbtn4()
{
Buy(250, GPU4, 1, 4);
}
public void GPUbtn5()
{
Buy(380,GPU5,1,5);
}
public void GPUbtn6()
{
Buy(470, GPU6, 1, 6);
}
//btn cpu
public void CPUbtn1()
{
Buy(100, CPU1, 0, 1);
}
public void CPUbtn2()
{
Buy(140, CPU2, 0, 2);
}
public void CPUbtn3()
{
Buy(190, CPU3, 0, 3);
}
public void CPUbtn4()
{
Buy(250, CPU4, 0, 4);
}
public void CPUbtn5()
{
Buy(380, CPU5, 0, 5);
}
public void CPUbtn6()
{
Buy(470, CPU6, 0, 6);
}
}
Answer by Bunny83 · Feb 04, 2018 at 09:54 PM
Parameters of a method are always copied or passed by value unless the method has a ref or out parameter where the parameter would be passed by reference. So changing the value of the parameter "a" inside the method does not have any effect on the variable / value that was passed to the method. You have to declare your method like this:
public void falsekek(ref bool a, int b, int c)
{
// ...
And now you have to call your method like this:
falsekek(ref GPU1, 1, 1);
This time "a" is a reference to a variable. Changing "a" inside the method will actually change the value stored in the variable that you passed to the method.
Your answer
Follow this Question
Related Questions
check if-statement/bool when button is pressed? 1 Answer
How to turn off/on a collider using a bool variable from another script 1 Answer
Cannot implicitly convert type `void' to `bool' 1 Answer
Toggle a bool on only one object at a time by tapping an object. 0 Answers
Boolean somehow becomes false. And an Integer zero. 0 Answers