transporting a bool across scene's while changing it

55 Views Asked by At

For my game you need to complete a mini-game to unlock abilities. But I atually have no clue how to do it cause the value gets resetted to false whenever I load the main-level. Code playerMovement:

static bool FistAttackEnabled;

void Update ()
{
        if (FistAttackEnabled == true)
        {
            if (Input.GetMouseButtonDown(0))
            {
                Debug.Log("Attack");
                PlayerMovement.SetFloat("Attacking", 1f);
                HitArea.SetActive(true);
            }
        }
}

Code miniGame:

    void Start()
    {
        FistAttackEnabled = Player.GetComponent<Player_Movement>().FistAttackEnabledPortable;
    }
    void Update()
    {         
            if (SheepsAmountGuess == NeededAni)
            {
                FistAttackEnabled = true;
            }
    }

But this doesnt work. I tried making a portable bool (FistAttackEnabledStatic = FistAttackEnabled) Because you cant transport static bool value's across scripts, but this also didn't work. Does anyone have a clue how to do it?

PS: The code is bigger but it doesn't have anything to do with the attack.

1

There are 1 best solutions below

0
On

Each time the scene is loaded the scripts are reloaded, so variables will go to their "default" state

You can avoid the destruction of the gameObject by usign DontDestroyOnLoad(this.gameObject);

Since the GameObject wont be deleted each time you reload the scene a new copy will be created to solve that you should use a singleton(look for it you will find information easyly)

Both things will solve the problem temporaly but once you close the game everything will go to the original state. You should use some method to save the progress, PlayerPrefs is a really easy way to do it.