Search Unity

  1. Unity 2019.1 is now released.
    Dismiss Notice

Understanding Singleton Patterns and Instancing Better

Discussion in 'Scripting' started by srhunity, Dec 8, 2018.

  1. srhunity


    Sep 12, 2017
    Hey everybody,

    As the title says, I am trying to learn Singleton Patterns and Instancing Better. In my game, I have a Prefab called "player" that duplicates when it transitions between scenes. I have written a script that uses instancing and attached it to the "player" so that it doesn't duplicate.

    The code works fine, I'm just confused as to what everything references to and would like some clarification. Here is my code (the script is called "PlayerController")

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour {
    3.     public Rigidbody2D theRB;
    4.     public float moveSpeed;
    5.     public Animator myAnim;
    6.     public string areaTransNamePlayer;
    8.    public static PlayerController instance;
    11.     private Vector3 bottomLeftLimit;
    12.     private Vector3 topRightLimit;
    13.     // Use this for initialization
    14.     void Start () {
    15.         if (instance == null)
    16.         {
    17.             instance = this;
    19.         } else
    20.         {
    21.             Destroy(gameObject);
    22.         }
    24.         DontDestroyOnLoad(gameObject);
    25.     }

    Here are my questions:

    - When I initiate the instance variable, does the type PlayerController refer to the actual script or to the script component on the Prefab?

    - Is the "instance" variable automatically set to null if it isn't defined during initialization?

    - What does "this" refer to in this case? the script or the script component?

    Thanks in advance to those that can help

    Attached Files:

  2. Lurking-Ninja


    Jan 20, 2015
    The PlayerController does not refer to anything other than your class. When you're writing a class, you basically create a new type in C#, just like string or int. It's a type, it's not component, it's not an object. It's just a type (sort of, when you advance, you will find that there is more to it, but for now, it's good enough to think of it this way). You can create and you can refer to this type of objects when you are done, like you are doing in your 8th line.
    <visibility qualifier> <type> <variable name> - this is how you create a class member variable in this case the type is PlayerController.

    The instance variable is implicitly set to null in the 8th. line. When you are defining a variable and you don't explicitly set it to a value (usually) it will be initialized with the default which appropriate to the type (reference types has the default value of null)

    It's referring to the Instance of the PlayerController type which is created by Unity automatically (you don't call the MonoBehaviour objects' constructor for this very reason), but this is exactly the same as the component on your game object.
    So this is referring to the actual instance of your class.

    Hope I gave you clear answers, I tried, I swear. :D
  3. srhunity


    Sep 12, 2017
    I think I understand now. So let me try to run through how the computer sees it:

    1. When the game starts, it loads all the predesignated Game Objects into the scene, including the "Player" GameObject.

    2. Since the "Player" loads into the scene, it creates an instance of its script component, which is essentially an instance of the PlayerController class(or "type" as you said).

    3. This causes the script to load. When the script loads, it sees that the "instance" variable is set to null.

    4. Thus, causing the PlayerController script to set the variable to "this", which refers to the actual "PlayerController" class (type).

    **if the instance is already set to "this", then it is no longer null, thus destroying any duplicates, correct?
  4. Lurking-Ninja


    Jan 20, 2015
    Yepp, you're correct. Roughly this is how the game objects and the components on them are initialized (there are a lot of other things, but they are irrelevant in this case).
  5. srhunity


    Sep 12, 2017
    Good enough for me. Thank you for the help!