Global objects & good practices?
First and foremost i'm prototyping a 2D Platformer and i've just started with the checkpoint mechanics. The way i'm handling checkpoints and world resets is that i made an object that acts as a CheckpointManager and i simply won't destroy it when the player resets because of dying, this object also handles spawning the player in the right place.
So far so good, what i think might be a bad practice is the way i'm accessing this object/script, i'd like some feedback and if there's a better way to access it.
checkpointManager = GameObject.Find ("Checkpoint Manager").GetComponent<CheckpointManager>();
This is how i do it, but this means i'll always have to look for it as a GameObject and since i have quite a few of them when it expands even further it might affect the performance.
Answer by JedBeryll · Aug 23, 2016 at 08:47 PM
It's faster to use FindWithTag and it's even faster to create a static reference to the manager. All my managers have references to themselves because there's always only one of them. This is my usual "formula":
public class CheckpointManager {
public static CheckpointManager Instance { get; private set; }
private void Awake() {
Instance = this;
}
}