Player Save Question

Hi community,

so I got a locker, if I interact with it, playerNormal is destroyed and playerSpaceSuit is instantiated and vice versa. I made a save script for health stat, but the health always aplies to either playerNormal or playerSpaceSuit. They never share the same stat. Please take a look at my scripts

       //This is the Global Instance that saves the health float
        public float health = 3;
        public static Tutorial Instance;
        void Awake()
        {
            if (Instance == null)
            {
                DontDestroyOnLoad(gameObject);
                Instance = this;
            }
            else if (Instance != this)
            {
                Destroy(gameObject);
            }
        }
    
    // This is the relevant code snippets from the playerHealth Script
    // Both playerNormal and playerSpaceSuits have an own playerHealth 
    // Script that are pretty similiar but not quite, both scripts have same code
    // snippets
    
        void Start ()
        {
            health = Tutorial.Instance.health;
            healthSlider.value = Tutorial.Instance.health;
        }
    
        public void SavePlayer()
        {
            Tutorial.Instance.health = health;
        }
    
        public void OnDestroy()
        {
            SavePlayer();
        }

So if playerSpaceSuit takes damage and interacts with the locker, playerNormal doesn’t have the health from playerSpaceSuit. If I interact with the locker again and playerSpaceSuit is instantiated, now he has the saved health stat

any Idea?

EDIT: Added Health Scripts:

Let me show you the exact scripts

// That is the playerNormal Health Script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine.UI;
    using UnityEngine;
    using UnityEngine.SceneManagement;
    
    public class PlayerHealth : MonoBehaviour {
    
        //public float startHealth = 3;
        public float health;
        public float maxHealth = 3;
    
        private PlayerScript playerScript;
        private bool isDead = false;
        private bool isDamageable = true;
    
        public GameObject player;
        public Transform playerPosition;
        public GameObject playerRagdollRight;
        public GameObject playerRagdollLeft;
        public Slider healthSlider;
        public GameObject player1Canvas;
        public int level = 0;
        /*public GameObject sliderStamina;
        public Slider healthSlider;
        public Image noAttack;
        public Image noStamina;
        public Image taunt;*/
        //public GameObject player1;
    
        void Start ()
        { 
            health = GlobalControl.health;
            healthSlider.value = GlobalControl.health;
            playerScript = GetComponent<PlayerScript>();
        }
    
        public void ApplyDamage(float damage)
        {
            if (isDamageable)
            {
                health -= damage;
    
                health = Mathf.Max(0, health);
    
                healthSlider.value -= damage;
    
                if (!isDead)
                {
                    if (health == 0)
                    {
                        isDead = true;
                        Dying();
                    }
                }
    
                isDamageable = false;
                Invoke("ResetIsDamageable", 2);
                //noAttack.enabled = true;
            }
        }
    
        public void AddHealth(float extraHealth)
        {
            health += extraHealth;
    
            health = Mathf.Min(health, maxHealth);
    
            healthSlider.value += extraHealth;
        }
    
        public void ResetIsDamageable()
        {
            isDamageable = true;
            //noAttack.enabled = false;
        }
    
        public void Dying()
        {
            player.SetActive(false);
            player1Canvas.gameObject.SetActive(false);
            /*sliderStamina.SetActive(false);
            healthSlider.gameObject.SetActive(false);
            noAttack.gameObject.SetActive(false);
            noStamina.gameObject.SetActive(false);
            taunt.gameObject.SetActive(false);*/
            if (playerScript.lookingRight == true)
            {
                Instantiate(playerRagdollRight, playerPosition.position, Quaternion.identity);
            }
            else if (playerScript.lookingRight == false)
            {
                Instantiate(playerRagdollLeft, playerPosition.position, Quaternion.identity);
            }
            playerScript.enabled = false;
            Invoke("RestartLevel", 3);
    
        }
    
        public void StartGame()
        {
            SceneManager.LoadScene(0);
        }
    
        public void OnDestroy()
        {
            SavePlayer();
        }
    
        public void SavePlayer()
        {
            GlobalControl.health = health;
        }
    
        public void RestartLevel()
        {
            //yield return new WaitForSeconds(4); if IEnumerator
            //health = startHealth;
            //isDead = false;
            //playerScript.enabled = true;
            SceneManager.LoadScene(level);
            //Instantiate(player1, playerPosition.position, Quaternion.identity);
        }
    }

//that is the playerSpaceSuit HealthScript
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
using UnityEngine.SceneManagement;

public class PlayerHealthSuit : MonoBehaviour
{
    //public float startHealth = 3;
    public float health;
    public float maxHealth = 3;
    public float oxygen = 100;
    public float oxygenLoss = 1f;

    private PlayerScriptSuit playerScriptSuit;
    private bool isDead = false;
    private bool isDamageable = true;

    public GameObject player;
    public Transform playerPosition;
    public GameObject playerRagdollRight;
    public GameObject playerRagdollLeft;
    public Slider healthSlider;
    public Slider oxygenSlider;
    public GameObject player1Canvas;
    public int level = 0;
    /*public GameObject sliderStamina;
    public Slider healthSlider;
    public Image noAttack;
    public Image noStamina;
    public Image taunt;*/
    //public GameObject player1;

    void Start()
    {
        health = GlobalControl.health;
        healthSlider.value = GlobalControl.health;
        playerScriptSuit = GetComponent<PlayerScriptSuit>();
    }

    void Update()
    {
        oxygen -= oxygenLoss * Time.deltaTime;
        oxygenSlider.value = oxygen;
        if (oxygen == 0 || oxygen <= 0)
        {
            isDead = true;
            Dying();
        }
    }

    public void ApplyDamage(float damage)
    {
        if (isDamageable)
        {
            health -= damage;

            health = Mathf.Max(0, health);

            healthSlider.value -= damage;

            if (!isDead)
            {
                if (health == 0)
                {
                    isDead = true;
                    Dying();
                }
            }

            isDamageable = false;
            Invoke("ResetIsDamageable", 2);
            //noAttack.enabled = true;
        }
    }

    public void AddHealth(float extraHealth)
    {
        health += extraHealth;

        health = Mathf.Min(health, maxHealth);

        healthSlider.value += extraHealth;
    }

    public void ResetIsDamageable()
    {
        isDamageable = true;
        //noAttack.enabled = false;
    }

    public void Dying()
    {
        player.SetActive(false);
        player1Canvas.gameObject.SetActive(false);
        /*sliderStamina.SetActive(false);
        healthSlider.gameObject.SetActive(false);
        noAttack.gameObject.SetActive(false);
        noStamina.gameObject.SetActive(false);
        taunt.gameObject.SetActive(false);*/
        if (playerScriptSuit.lookingRight == true)
        {
            Instantiate(playerRagdollRight, playerPosition.position, Quaternion.identity);
        }
        else if (playerScriptSuit.lookingRight == false)
        {
            Instantiate(playerRagdollLeft, playerPosition.position, Quaternion.identity);
        }
        playerScriptSuit.enabled = false;
        Invoke("RestartLevel", 3);

    }

    public void StartGame()
    {
        SceneManager.LoadScene(0);
    }

    public void OnDestroy()
    {
        SavePlayer();
    }

    public void SavePlayer()
    {
        GlobalControl.health = health;
    }

    public void RestartLevel()
    {
        //health = startHealth;
        //isDead = false;
        //playerScript.enabled = true;
        SceneManager.LoadScene(level);
        //Instantiate(player1, playerPosition.position, Quaternion.identity);
    }
}

//and thats the globalControl Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GlobalControl : MonoBehaviour {

    //private PlayerHealth pHealth;
    //private PlayerHealthSuit pHealthSuit;
    //public int playerStartHealth = 3;
    //public GameObject gameOverScreen;
    public static float health = 3;

    public static GlobalControl Instance;

    void Awake()
    {
        if (Instance == null)
        {
            DontDestroyOnLoad(gameObject);
            Instance = this;
        }
        else if (Instance != this)
        {
            Destroy(gameObject);
        }
    }

}

If you have 1 class with a non static field that means it’s for that class only.

When you make 2 players and assign each the same script that means the class is instantiated twice.

Once for player one and player 2, so both objects have their own fields and properties.

What you want to do is make the health variable a static which means it’s shared through all it’s objects like so:

public static float health = 3;

However, making it static means it won’t be visible in the inspector any longer.

@ShadyProductions
thank you for your time, where do I put the static float? on the playerHealth script or in the global instance? if I make it in the global instance, I have to make everthing …Tutorial.health… in the playerHealth scripts or on the playerHealth scripts or on both, tried every way, still the same problem.
I forgot to mention my playerHealth Scripts also named differently for each Player

@ShadyProductions
Still doesnt work, I tried to put static in playerhealth scripts, in globalcontrol script or in both, it doesnt work, I forgot to mention that I have two different names for the health scripts either playerNormal or playerSpaceSuit

@DuB86
EDIT from PC:
I have code for a solution with static variable (as someone already suggested).
This is a global storage in C#

using System.Collections;
using System.Collections.Generic;
//deleted: using UnityEngine;

//deleted: : MonoBehaviour (It does not need to be attached on object)
public static class GlobalValueStorage {
	public static int playerHealth;
}

How to use it:

GlobalValueStorage.playerHealth = hp; //set value (safe)
Debug.Log (GlobalValueStorage.playerHealth); //get value (load)
//Values are not persistent between scenes

BEFORE EDIT:

You may find a way around it by setting the health of new object from old one and then delete it.
Code looks like:

Instantiate(playerRagdollLeft, playerPosition.position, Quaternion.identity).GetComponent<YourHealthScriptName>().SetLife(hp);
//You get component and in that component create function:
public void SetLife(int healht){
    hp = health;
}

(Sorry for short answer, I’m on phone)