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);
}
}
}