Home > Mobile >  How can I use a bool value in class in a method which has changed in another method?
How can I use a bool value in class in a method which has changed in another method?

Time:04-09

 public class CustomizationUI : MonoBehaviour
 {
private bool ifTGOn = false;
private bool ifTBOn = false;

......(some variable declaration)
int tg=0;
int bg=0;
int tb=0;
int bb=0;
string gender = "boy";
   public async void loadTopMaterialCards()
{
    if (gender == "boy"){
        if (tb==0){
    contentPanelTB.SetActive(true);
    contentPanelBB.SetActive(false);
    ifTBOn = true;
    Debug.Log("I ammmmmmmmmmmm ifTBON" ifTBOn);
    for (int i=0; i<topBoyMaterials.Count; i  ){
        
    AsyncOperationHandle<GameObject> handle = Addressables.InstantiateAsync("MaterialCard");
    await handle.Task;

    GameObject matcard = handle.Result;
    matcard.transform.SetParent(ViewPanelTB);
    MaterialCard mcard = matcard.GetComponent<MaterialCard>();
    mcard.image.sprite = topBoyImages[i];
    mcard.material = topBoyImages[i].name;
    }
    tb  ;
    }else if (tb==1){
        contentPanelTB.SetActive(true);
        contentPanelBB.SetActive(false);
        ifTBOn = true;
    }
    }
    
    Debug.Log("I am TG and TB at the second time" ifTGOn ifTBOn);
}
public async void loadBottomMaterialCards()
{
    Debug.Log("I am TG and TB at the third time" ifTGOn ifTBOn);
    if (gender == "boy"){
        if (bb==0){
    contentPanelTB.SetActive(false);
    contentPanelBB.SetActive(true);
    ifTBOn = false;
    for (int i=0; i<bottomBoyMaterials.Count; i  ){
        
    AsyncOperationHandle<GameObject> handle = Addressables.InstantiateAsync("MaterialCard");
    await handle.Task;

    GameObject matcard = handle.Result;
    matcard.transform.SetParent(ViewPanelBB);
    MaterialCard mcard = matcard.GetComponent<MaterialCard>();
    mcard.image.sprite = bottomBoyImages[i];
    mcard.material = bottomBoyImages[i].name;
    }
    bb  ;
    }else if (bb==1){
        contentPanelTB.SetActive(false);
        contentPanelBB.SetActive(true);
        ifTBOn = false;
    }
    }
    Debug.Log("I am TG and TB at the fourth time" ifTGOn ifTBOn);
}
public void changeMaterial(){
    Debug.Log("HIIII iam panellll" ifTBOn);
}
void Awake(){
    instance = this;
}

// Start is called before the first frame update
void Start()
{
   
}

// Update is called once per frame
void Update()
{
    
}

} In the method loadTopMaterialCard() I set the bool ifTBOn to true after press a button(in unity), the debug inside loadTopMaterialCard did show that it has been set to be true, however, I try to debug in changeMaterial() after ifTBOn has been set to true, but it show false. This is the whole piece of the class, I don't know why the bool is always false when debug it in the class changeMaterial() How can I fix it?

(I dont think there is someplace in my code to edit the bool back to false

CodePudding user response:

The problem here is likely that your booleans aren't being set to true in the first place. There should be no problem accessing it in multiple methods.

I'd step through this code with breakpoints and ensure that your if statements are passing and the code is reaching the point where it can set the boolean to true.

If it is, then there must be somewhere else in the code that you're setting them back to false.

Outside of those two things, we'd need to see the second method you're accessing them in and any other code that touches the fields.

CodePudding user response:

Sorry if I am picky, but I strongly recommend not to use such variable names. In my opinion are misleading, meaningless and quite ugly. I hope you can find better names in the future.

A part of that, you have loadBottomMaterialCards method, in which you set ifTBOn to false.

I presume this method is execute without your knowledge. Try to set a breakpoint there and see if the instruction pointer goes there.

Good luck and remember the names <3

  • Related