Videodaki gibi restart butonuna basınca ne skor çalışıyor ne de nesne geliyor. Bende anlamadım. Bayağıdır uğraşıyorum. Restart butonunun Click alanındaki çalışan kod. Newgame fonksiyonu seçili koddur.
C#:
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
[DefaultExecutionOrder(-1)]
public class GameManager : MonoBehaviour.
{
public static GameManager Instance { get; private set; }
public float initialGameSpeed = 5f;
public float gameSpeedIncrease = 0.1f;
public float gameSpeed { get; private set; }
[SerializeField] private Text scoreText;
[SerializeField] private Text hiscoreText;
[SerializeField] private Text gameOverText;
[SerializeField] private Button retryButton;
private Player player;
private BarrierSpawner spawner;
private float score;
public float Score => score;
private void Awake()
{
if (Instance != null) {
DestroyImmediate(gameObject);
} else {
Instance = this;
}
}
private void OnDestroy()
{
if (Instance == this) {
Instance = null;
}
}
private void Start()
{
player = FindObjectOfType<Player>();
spawner = FindObjectOfType<BarrierSpawner>();
NewGame();
}
public void GameOver()
{
gameSpeed = 0f;
enabled = false;
player.gameObject.SetActive(false);
spawner.gameObject.SetActive(false);
gameOverText.gameObject.SetActive(true);
retryButton.gameObject.SetActive(true);
UpdateHiscore();
}
public void NewGame()
{
BarrierMove[] obstacles = FindObjectsOfType<BarrierMove>();
foreach (var obstacle in obstacles) {
Destroy(obstacle.gameObject);
}
score = 0f;
gameSpeed = initialGameSpeed;
enabled = true;
player.gameObject.SetActive(true);
spawner.gameObject.SetActive(true);
gameOverText.gameObject.SetActive(false);
retryButton .gameObject.SetActive(false);
UpdateHiscore();
}
private void Update()
{
gameSpeed += gameSpeedIncrease * Time.deltaTime;
score += gameSpeed * Time.deltaTime;
scoreText.text = Mathf.FloorToInt(score).ToString("D5");
}
private void UpdateHiscore()
{
float hiscore = PlayerPrefs.GetFloat("hiscore", 0);
if (score > hiscore)
{
hiscore = score;
PlayerPrefs.SetFloat("hiscore", hiscore);
}
hiscoreText.text = Mathf.FloorToInt(hiscore).ToString("D5");
}
}
Son düzenleyen: Moderatör: