2
\$\begingroup\$

I'm viewing tutorial of Unity. In this tutorial, I see this piece of code:

public class GameControl : MonoBehaviour {
    public static GameControl control;

    public void Awake() {
        if (control == null) {
            DontDestroyOnLoad(gameObject);
            control = this;
        } else if (control != this) {
            Destroy(gameObject);
        }
    }
}

I don't understand above code very much. I just understand the first if. it means: when we first use this class, we will initialize control object and set attribute dontdestroyonload for this. But I don't understand second if, when will it meet condition control != this ?

Thanks :)

\$\endgroup\$
5
  • 1
    \$\begingroup\$ The code would be more understandable if it just used else instead of else if (control != this). Cf. principle of least astonishment. \$\endgroup\$
    – Eric
    Mar 1, 2015 at 20:38
  • 1
    \$\begingroup\$ @Eric Except, the next time the scene changes, control is no longer null, and the object containing the GameControl script would be destroyed. There's a reason it's else if. \$\endgroup\$
    – House
    Mar 1, 2015 at 22:34
  • \$\begingroup\$ @Byte56 Awake is called once in a script instance's lifetime, not on each scene change. So the first GameControl instance would assign itself to GameControl.control in its Awake and any further GameControl instances will destroy themselves in their Awakes. \$\endgroup\$
    – Eric
    Mar 2, 2015 at 8:47
  • 1
    \$\begingroup\$ @Eric You're correct, in this case it would make more sense to just use else. \$\endgroup\$
    – House
    Mar 2, 2015 at 16:14
  • \$\begingroup\$ so. we really don't need second else ? this code I wrap from Unity's tutorial. It means it is written by Unity Programmer. So hardly to think no effect for second else if. \$\endgroup\$
    – hqt
    Mar 2, 2015 at 17:03

2 Answers 2

2
\$\begingroup\$

This is the implementation of a very simple version of a programming pattern called Singleton. For more context, see Unity Singleton Pattern.

The basic idea is, the designer wants that there should only be one instance of the GameControl object. Therefore, before everything else (in Awake() function), the designer checks if the object is initialized or not. This first case of if the case that the object is not initialized, thus being the first instance of the object.

The second case is, the scene is changed and the same object is present in the new scene hierarchy, therefore control is already assigned and thus not equal to this. In this case, remove the newly created object from the scene.

\$\endgroup\$
1
\$\begingroup\$

The control variable is a static variable. The code you're looking at is for ensuring there's always a GameControl object in the world, and only one.

The first time a GameControl object is loaded, control is set. If a GameControl is ever attempted to be loaded again, it will not match the already loaded control object and will be destroyed.

\$\endgroup\$
6
  • \$\begingroup\$ Can you tell me. gameObject stands for which object ? I though it stands for GameControl itself. but as your explanation, it's not. right ? \$\endgroup\$
    – hqt
    Mar 1, 2015 at 19:13
  • \$\begingroup\$ In this case, gameObject is short for this.gameObject. It's the GameOject that belongs to the script currently running. Remember that scripts are attached to game objects, so the GameControl is not an object on its own, but rather a script attached to a game object. \$\endgroup\$
    – House
    Mar 1, 2015 at 19:16
  • \$\begingroup\$ Ah. that's object in unity scene that we often attach script into. So, why DontDestroyOnLoad(gameObject) helps saving gameObject (and GameControl object attach to its) when changing scene but OnDestroy(gameObject), GameControl object doesn't destroy too ? (I guess GameControl object doesn't destroy, because if it destroyed, singleton pattern is failed in this case) \$\endgroup\$
    – hqt
    Mar 1, 2015 at 19:22
  • \$\begingroup\$ DontDestroyOnLoad preserves an object between scene changes. Destroy (not OnDestroy, which is an event that's called when an object is destroyed) removes the game object from the scene. The singleton pattern is preserved because once a GameControl object is created, it can only be destroyed if you explicitly destroy it, and not accidentally through a scene change. \$\endgroup\$
    – House
    Mar 1, 2015 at 19:25
  • \$\begingroup\$ oh. so why should we use DontDestroyOnLoad and Destroy in this case ? Because as you see, GameControl object can only be destroyed if i explicitly destroy it. thanks :) \$\endgroup\$
    – hqt
    Mar 1, 2015 at 19:30

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .