Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Instance, what does it mean?

Discussion in 'Scripting' started by ScamTheMan, Oct 17, 2018.

  1. ScamTheMan

    ScamTheMan

    Joined:
    Oct 4, 2018
    Posts:
    75
    Code (CSharp):
    1. public class GameControl : MonoBehaviour {
    2.     public static GameControl instance;
    3.  
    4.     private void Awake()
    5.     {
    6.         if(instance == null)
    7.         {
    8.             instance = this;
    9.         }
    10.     }
    11. }
    Can you please explain me, what does this piece of code means and where do you use it? Thank you
     
    iydrss likes this.
  2. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    333
    This is a simple way to define a singleton, (it is actually missing some checking code but it works as it is).

    What this code does step by step:
    1. Declares a class by name
      GameControl 
      which extends
      MonoBehaviour
      , this gives
      GameControl 
      access to
      MonoBehaviour 
      methods and "behaviors"
    2. Defines a public static variable called instance of type
      GameControl
      , this variable can be accessed anywhere in the code just by calling
      GameControl.instance
      .
    3. On the Awake method (is called when the object it placed in the scene) it is checking if there is already an instance for
      GameControl 
      in the variable instance, if there isn't any then it assigns itself to instance.
    This class by itself doesn't give you much, but you can add things to it like,
    public GameObject playerObject
    , and assign a player to it via inspector. Doing so will allow you do access the player's gameobject from anywhere in your code (as long as it is in the same namespace, which is not defined so its fine). You can simply do
    GameControl.instance.playerObject
    and access it, instead of using Find, or other ways to find objects.
     
    Acktrol, kobozev, omern92 and 4 others like this.
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Basically, an instance is one particular copy of a thing. "Toyota Camry" is a class; "the Camry parked in your driveway" is an instance of that class.

    In this code snippet, this is a script that is designed to have only one instance at any given time - this is known as a singleton. In Unity, singletons are useful because they're globally accessible (like a static class, think Mathf for example) but can still run Update etc functions every frame, have object references assigned in the inspector, etc, like regular objects.
     
  4. ScamTheMan

    ScamTheMan

    Joined:
    Oct 4, 2018
    Posts:
    75
    Thank you. I know there is missing a top piece and something to class to work with, but I just wanted to know what means that instance and where do I use it for.
     
  5. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    "instance" is simply a name assigned to a reference. You could call it whatever you want; "peanutButter", "alligator", "butterscotchPie", etc. "instance" is just the name that is normally given to singletons.

    You call the "instance" of a singleton whenever you need to access something from that class.
    There could be many reasons why you'd use a singleton, and in some cases, there are alternatives, but the intended use of them is to create & interact with exactly one instance of an object, and because this instance is static, it can be referenced in any script by just typing "<class-name>.instance".

    A GameManager script is an example where you may want to use a singleton, because you usually only ever have one GameManager, and other objects usually need to reference the values that it holds.
     
  6. Stardog

    Stardog

    Joined:
    Jun 28, 2010
    Posts:
    1,886
    They want to be able to access the class members (variables/functions) by typing ClassName.MemberName, but C# won't allow that unless it's static, but if it's static then you can't access the other members...

    So they make this static instance that can be accessed as ClassName.Instance.MemberName... It can access all of the class's members.

    Now it can be used from any script like GameControl.instance.StartGame().
     
    kingsdigitalagency likes this.
  7. BikaYoBadi

    BikaYoBadi

    Joined:
    Jan 1, 2016
    Posts:
    50
    Sorry to wake the post up, but i have a question about this. Is it better to have a static instance than a normal instance which is declared by ayn find or get methods. For example, let's say i have around 10 15 scripts that have reference to a game manager to use some variables or etc. Should game manager have static instance ? Which approach is better ? Is having 10 15 same reference on each script better or just a waste of memory ? Thanks in advance.
     
  8. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    333
    Depends on your definition of better, if you mean performance, yes, static skips some stack calls, so it is faster on that. It will also save some GC when used properly. If you mean organizational, it really depends on what you want to do.

    When it comes to static, there is no such a thing as an "instance", they exist uniquely within the context declared, so you can't have such a thing as a instances of static variables, they exist where they are declared and don't require class instance to function. (ofc they do require object instantiation, but that's something different)

    When you say you have 15 scripts calling, on several places in each script I assume, the way I recommend doing is having a local reference to your singleton instance (static reference or not, really depends on what you want to do) in each class, which receives the reference from the singleton on the start of the script (Start, Awake, Lazy initialization or Contructor if possible).
    Then each script would access its local shortcut to the singleton instance.

    The reasons behind it:
    First, it's easier to type a local reference than an external reference, like playerManager.DoSomething(), rather than PlayerManaget.GetSingletonInstance().DoSomething(); (Keep in mind once you have the reference, there call cost on that reference is only 1+methods called)
    Second, it saves on call stacks, the performance saving costs will really depends on the path distance to reach the singleton.

    Keep in mind that, unless you're doing thounsands/millions of operactions per frame, that's really not going to make any feasible difference in performance/memory.
     
    livium and BikaYoBadi like this.
  9. BikaYoBadi

    BikaYoBadi

    Joined:
    Jan 1, 2016
    Posts:
    50
    So, i should use local references to have a slight more performant game ?
     
  10. unity_12343303

    unity_12343303

    Joined:
    Mar 25, 2019
    Posts:
    1
    can play with you your youser name tome plese
     
  11. FernandoHC

    FernandoHC

    Joined:
    Feb 6, 2018
    Posts:
    333
    @BikaYoBadi to put it simple, yes.
    To put it in reality, yes and no, there is no one-size-fits-all when you talk about performance, it will always be specific to your project as a whole. Ofc you can fine tune small methods as long as they are completely isolated, but it will still depend on how they are using in the project.

    My suggestion to you is, don't worry about performance until you have a performance issue. I know this sounds counter productive, as you will have to go back and fix stuff, but the kind of thing you're asking comes only with experience from doing that kind of thing.
     
    BikaYoBadi likes this.
  12. BikaYoBadi

    BikaYoBadi

    Joined:
    Jan 1, 2016
    Posts:
    50
    Thank you so much for your time.
     
  13. iydrss

    iydrss

    Joined:
    Apr 22, 2020
    Posts:
    4


    Thank Youu!!!