Unity 5.6 - Game Manager jako Singelton

0

Witam

W swoim projekcie Unity 5.6 próbuje utworzyć klasę która będzie zarządzać grą.
Wydaje się że to się udało, problem natomiast leży w odwołaniu się z innej klasy do mojego GameManagera.
Unity wypluwa mi: Object reference not set to an instance of an object.

Jestem nowy w C# więc nie mam doświadczenia i nie umiem sobie sam z tym poradzić a kody z internetu i w ogóle podpowiedzi w nim zawarte tez nie sa zbyt pomocne - chyba dotyczą innej wersji...

Kod:
GameManager

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GameManager : MonoBehaviour {
	private static GameManager _instance;

	public static GameManager Instance { get { return _instance; } }


	private void Awake()
	{
		if (_instance != null && _instance != this)
		{
			Destroy(this.gameObject);
		} else {
			_instance = this;
		}
	}

	public void loguj()
	{
		Debug.Log ("aaaa");
	}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ColiderDetectorScript : MonoBehaviour {

	// Use this for initialization
	void Start () {
		
	}
	
	// Update is called once per frame
	void Update () {
		
	}

	void OnMouseDown()
	{
		Debug.Log ("Cos klilo");
		GameManager.Instance.loguj (); <-- tu pokazuje błąd.
	}
}

0

Masz źle napisanego tego singletona. Spójrz tu: https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager
Ważne jest" DontDestroyOnLoad.

Powinieneś to zrobić analogicznie do tego kodu.

1 użytkowników online, w tym zalogowanych: 0, gości: 1