My struggle with a Check Point Manager

My current project needed a manager to record where the players need to respawn in the event of their death.

While it was unlikely that they could actually die in the Tutorial area of the game, I made sure that they would still respawn at the start of the level. When it came to the players going though a trigger to change the spawn point, I found that just using the following script, wouldn’t work.

using UnityEngine;
using System.Collections;

public class DontDestroyMe : MonoBehaviour {

// Use this for initialization
void Start () {
DontDestroyOnLoad (gameObject);
}

// Update is called once per frame
void Update () {

}
}

While this instance doesn’t destroy itself, it actually creates another instance of the CheckPointManager which seems to take priority over the current instance of the script and returns null object exceptions when trying to find the individual check points.

My research on the internet eventually led me to this site : http://unitypatterns.com/singletons/

This led me to using this for my CheckPointManager:

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

public class CheckPointManager : MonoBehaviour
{
private static CheckPointManager _inst;

public static CheckPointManager inst
{
get
{
if(_inst == null)
{
_inst = GameObject.FindObjectOfType<CheckPointManager>();
//Don’t destroy
DontDestroyOnLoad(_inst.gameObject);
}
return _inst;
}

}

public List<GameObject> checkPoints = new List<GameObject>();
public GameObject atlCharacter;
public GameObject ollinCharacter;

public bool spawnPoints = true;
public bool firstCheckpoints = false;
public bool secondCheckpoints = false;

void Awake()
{
if(_inst == null)
{
//If I am the first instance, make me the Singleton
_inst = this;
DontDestroyOnLoad(this);
}
else
{
//If a Singleton already exists and you find
//another reference in scene, destroy it!
if(this != _inst)
DestroyImmediate(this.gameObject);
}
}

 

Through testing, I found that when the game returned to the menu, the CheckPointManager was still operating and throwing Null object reference exceptions.  I modified the awake function to the following:

 

void Awake()
{
if(_inst == null)
{
//If I am the first instance, make me the Singleton
_inst = this;
if(Application.loadedLevel == 0)
DestroyImmediate(this.gameObject);
else
DontDestroyOnLoad(this);
}
else
{
//If a Singleton already exists and you find
//another reference in scene, destroy it!
if(this != _inst)
DestroyImmediate(this.gameObject);
}
}

The next problem was with the actual checkpoints.  as they were being destroyed, I ended up using the first “DontDestroyMe” script on the empty game object that was the parent of the individual check points.

This ended up working perfectly, for this project, at least, but I think it should work on any future game.

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s