я использовал сохранения (PlayerPrefs) взял туториал с ютуба
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using System;
public class PlayerMove : MonoBehaviour
{
[SerializeField] float gravity = 50;
[SerializeField] Text HpText;
[SerializeField] float jumpForce = 30;
[SerializeField] private Text scoreText; //ссылка на текст подсчет очков
[SerializeField] private Text scoreTextpremium; //ссылка на текст подсчет особых очков
private bool facingRight = true;
private float moveInput;
private int scoreCount = 0;
private int scoreCountP = 0;
private Rigidbody2D rb;
private int health;
public float speed = 10f;
private Vector2 moveVector;
Vector3 direction;
Animator anim;
GameObject Player;
public void ChangeHealth(int count)
{
health = health += count;
if (health > 100)
{
health = 100;
}
if (health <= 0)
{
LoadSceneLose();
}
HpText.text = health.ToString();
}
void Start()
{
anim = GetComponent<Animator>();
PlayerPrefs.GetInt("health", health);
ChangeHealth(100);
}
void Awake()
{
rb = GetComponent<Rigidbody2D>();
}
void FixedUpdate()
{
if(facingRight == false && moveInput > 0)
{
Flip();
}
else if(facingRight == true && moveInput <0)
{
Flip();
}
moveVector.x = Input.GetAxis("Horizontal");
moveVector.y = Input.GetAxis("Vertical");
rb.MovePosition(rb.position + moveVector * speed * Time.deltaTime);
direction.y -= gravity * Time.deltaTime;
Flip();
}
public void LoadSceneOne()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 1);
}
public void LoadSceneLose()
{
SceneManager.LoadScene(5);
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag == "hleb")
{
Destroy(collision.gameObject);
health += 10;
HpText.text = health.ToString();//обновляем UIText
PlayerPrefs.SetInt("health", health);
}
if (collision.tag == "premium")
{
Destroy(collision.gameObject);
health += 50;
HpText.text = health.ToString();//обновляем UIText
PlayerPrefs.SetInt("health", health);
}
if (collision.tag == "door")
{
LoadSceneOne();
}
if (collision.tag == "dangerhleb")
{
GetComponent<PlayerMove>().ChangeHealth(-50);
StartCoroutine(Idle());
PlayerPrefs.SetInt("health", health);
}
if (collision.tag == "platform")
{
StartCoroutine(Idle());
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == "dangerhleb")
{
StopCoroutine(Idle());
}
}
private void OnCollisionStay2D(Collision2D other)
{
if(other.collider.tag == "boss")
{
GetComponent<PlayerMove>().ChangeHealth(-1);
PlayerPrefs.SetInt("health", health);
}
if(other.collider.tag == "enemy")
{
ChangeHealth(-1);
PlayerPrefs.SetInt("health", health);
}
if (other.collider.tag == "platform")
{
StartCoroutine(Idle());
PlayerPrefs.SetInt("health", health);
}
}
IEnumerator Idle()
{
while(true)
{
yield return new WaitForSeconds(1f);
GetComponent<PlayerMove>().ChangeHealth(-3);
}
}
void Flip()
{
if(Input.GetAxis("Horizontal") > 0)
{
transform.localRotation = Quaternion.Euler(0, 0, 0);
}
if(Input.GetAxis("Horizontal") < 0)
{
transform.localRotation = Quaternion.Euler(0, 180, 0);
}
}
}
в итоге вышли такие ошибки