- Home /
Keeping variables and bools across scenes
I've been looking everywhere to find a solution to my problem but I can't find it anywhere. I have a first-person game with a custom character controller script etc. which also has a fully functioning weapon/melee system with a bat as the physical weapon. With the weapon, the player needs to pick it up to make it active and I want to make it so that the player keeps the weapon when the next level is loaded and if they don't find the weapon they don't get it in the next level. I've tried DontDestroyOnLoad to no avail. I'm also about to start implementing a CoD-like health system (gets blood on the screen when hurt) and wanted to save this health when changing levels also. Could anyone help me?
Here are the relevant parts of my script:
Melee System:
if(weaponScript.GotWeapon == true)
{
TheSystem.active = true;
}
else
{
TheSystem.active = false;
}
And the pickup weapon script:
void OnTriggerEnter (Collider collision)
{
Debug.Log("TriggerEntered");
if(collision.GetComponent<CharacterController>() != null)
On = true;
}
if(On)
{
if(Input.GetKeyDown(KeyCode.E))
{
Debug.Log("EPressed");
EPress = true;
GotWeapon = true;
Destroy (physicalWeapon.gameObject);
}
gUIText.guiText.enabled = true;
}
else
{
gUIText.guiText.enabled = false;
}
if(EPress == true)
{
On = false;
}
Answer by HarshadK · Apr 04, 2014 at 11:16 AM
Check out this nice Unity tutorial from their live training archive:
Persistence - Saving and Loading Data
In this tutorial he has stressed on use of data serialization for a situation similar to that of yours.
I insist on taking some time out to watch this tutorial
Hope it helps.
I watched the video and it helped for a few other bits I needed to save however it didn't help with my weapon problem. I think it might have something to do with the way I've scripted it but I can't find anywhere that would cause it to not save the bool GotWeapon to the next level... Could it be the way I'm making the weapon disappear? the "TheSystem.active = false" etc. ?