T
T
Timofey Shestopalov2021-10-14 16:02:58
Unity
Timofey Shestopalov, 2021-10-14 16:02:58

How to set up Singleton?

I made a C# GameManager class:

spoiler

public class GameManager : MonoBehaviour
{
public static GameManager instance { get; private set; }
public bool GameOver = false;

private void Awake()
{
if (instance == null)
{
instance = this;
DontDestroyOnLoad(this.gameObject);
return;
}

Destroy(this.gameObject);
}

public void GameIsOver()
{
instance.GameOver = true;
}

public void StartGame()
{
instance.GameOver = false;
}
}


In the player class, on collision, I call the GameIsOver() method:
spoiler

private void OnCollisionEnter2D(Collision2D collision)
{
GameManager.instance.GameIsOver();
SwimButton.SetActive(true);
MenuButton.SetActive(true);
}


When the player collides with an object, the game checks the GameOver variable and stops the obstacles and the player falls. But the problem is that for the first two seconds everything goes fine, and then the unit completely becomes inoperative, and it must be turned off through the task manager... What could be wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
G
GavriKos, 2021-10-14
@GavriKos

It is unlikely that singleton is to blame here. Surely some kind of garbage in the update is happening

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question