Загрузка данных


using UnityEngine;
using TMPro;

public class PlayerController : MonoBehaviour
{
    [SerializeField] float rotationSpeed = 50f;
    [SerializeField] float raycastDistance = 1f;
    [SerializeField] LayerMask groundLayer;
    [SerializeField] GameObject cubePrefab;
    [SerializeField] int step = 5;
    [SerializeField] int counter;

    bool isGrounded;

    void Update()
    {
        CheckGrounded();
        transform.Rotate(0, 0, rotationSpeed * Time.deltaTime);

        if (Input.GetKeyDown(KeyCode.Space) && isGrounded)
            SpawnCube();
    }

    void CheckGrounded()
    {
        RaycastHit2D hit = Physics2D.Raycast(transform.position, Vector2.down, raycastDistance, groundLayer);
        isGrounded = hit.collider != null;
    }

    void SpawnCube()
    {
        counter += step;

        GameObject cube = Instantiate(cubePrefab, transform.position + Vector3.up * 1.5f, Quaternion.identity);

        cube.GetComponent<SpriteRenderer>().color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.8f, 1f);

        TextMeshPro txt = cube.GetComponentInChildren<TextMeshPro>();
        txt.text = counter.ToString();
        txt.color = Random.ColorHSV(0f, 1f, 0.7f, 1f, 0.8f, 1f);

        cube.AddComponent<CubeRotator>().speed = rotationSpeed;
    }
}