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. Dismiss Notice

What is an 'Instance'?

Discussion in 'Scripting' started by DarkEcho, Nov 12, 2014.

  1. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Ever video, document, tutorial I come across, I keep hearing of the term 'Instance'. Google'd it and Unity dosnt support this term.

    What is this 'Instance'?
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    "Instance" is a general language concept, not strictly limited to programming, and refers to one specific occurrence of a thing, idea, or situation.

    In programming, an instance is usually created as a copy of some template, which can that have it's properties modified to make it distinct and unique from other instances.

    Unity very much supports instances, eg. via Instantiate(...)
    (instantiate is the transitive verb for creating an instance)
     
    aulaul179 likes this.
  3. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Soooo going right down to the core here, its basically an Entity (gameobject, script, class etc)

    A yes or No is my preferred answer :) (Takes me awhile to get my head around things)
     
  4. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,377
    yes and no... you're answer isn't wrong, but it isn't exactly right either. Sorry I can't give you your preferred answer. I COULD leave it at "NO", but then you wouldn't know.

    It's not a script, it's an instance of a script. It's not a class, it's an instance of a class. Each gameobject in your scene is an instance of a GameObject.

    It's like you are an instance of human. You are A human... you are not THE human.

    That gameobject isn't THE gameobject, it is 1 of many gameobjects.

    The code you write is the THE, definite article. Where as you instantiate, make instances of, duplicate, whatever english word you want to say, things that are of the type of that code organized together as a script/class.
     
  5. Korno

    Korno

    Joined:
    Oct 26, 2014
    Posts:
    518
    Hey DarkEcho,

    To visualize an instance think about people. All people have properties - age, name, height etc. A list of properties a person has is our People template (a template is a class in programming)

    A instance is one unique version of this template that has been created with values for the properties ( a person with a name, age etc)

    My person template Person - Name, Age,

    An "instance" of a Person - John, 21
    Another "instance" of Person - Mark, 45

    If you are new to scripting you might want to check out :
    http://www.wikihow.com/Understand-Object-Oriented-Programming

    And about entities - Kind of. A game object is an instance of a GameObject template which contains instances of many other templates (rigid bodies, scripts, mesh renderes etc)
     
  6. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    Ah i see now!
    So in short, an Instance is just one of many of an entity. (1 of many gameobjects, scripts etc)

    Cheers!

    Btw, sorry if I appeared rude/demanding in the following text, some tend to drone on into the advanced stuff rather than making sure you understand the basic stuff. I hope i havnt caused offence.

    Anyway thank you!
     
    sfrederick4976 likes this.
  7. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    You never played a game with dungeon (raid) instances?
     
  8. DarkEcho

    DarkEcho

    Joined:
    Jul 7, 2014
    Posts:
    231
    GW2 Instances comes to mind, no worrys i get it now
     
  9. ZO5KmUG6R

    ZO5KmUG6R

    Joined:
    Jul 15, 2010
    Posts:
    489
    I don't quite know how to describe it. But I can do it with code
    Code (CSharp):
    1. //Create a new instance of color called c
    2. Color c = new Color(1,1,1,1);
    3. //Create a new instance of a GameObject called go
    4. GameObject go = new GameObject();
     
  10. a906891

    a906891

    Joined:
    Aug 7, 2018
    Posts:
    4
    it is not A instance ....it is An instance .....but very good answer thanks
     
  11. csofranz

    csofranz

    Joined:
    Apr 29, 2017
    Posts:
    1,556
    Seriously?

    If you feel the need to correct the occurence of one instance (see what I did here?) of "an instance" (Korno did spell it correctly multiple times in their post) in a 5 years old thread you need help. Urgently.

    How many people would enjoy helpling others, only to be rebuked for their spelling? This forum is Content Over Form. By an infinite margin.

    May I recommend you head over to SelfimportantSpellingNazis.com for a good time?

    Sheesh.
     
    h-dragon919, SparrowGS and Munchy2007 like this.
  12. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,731
    No capital letter at start of sentence, missing commas, incorrect usage of ellipsis and no full stop.

    An ellipsis is 3 dots, not four or five.

    Pot, kettle, black? :p

    I've no doubt there's grammatical errors in my comment also, but I didn't start this :)
     
    Last edited: Jul 3, 2019
    lordofduct likes this.
  13. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,911
    But since it is re-opened and the older answers weren't quite right:

    "Instance" is a way to distinguish a script you write, which is like an idea, from when you put it on gameObjects, where it's like a real thing. Those aren't quite copies of the script -- they're real things using the script, so we call them an instance of it.

    Say you write a script "public ball : MonoBehaviour { public int points; ... }". There are no instances of it yet, so you can't change the point value. If you put that script on two balls, you now have two instances. You still can't set _the_ point value of balls. But you can select one instance and set points for that one, like ball1.points=5;. Thinking about them as instances is also a way to see that each script on a gameObject has its own copy of the variables.
     
    SSSHINNN likes this.
  14. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    The easiest way for me to think of it is with a car analogy. "Toyota Corolla" is a class. "The Toyota Corolla that is in your garage" is the instance.
     
    lordofduct likes this.
  15. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,911
    I've found that for code, real-world examples are good to get the general idea, and fine for explaining to non-coders. But they break down so quickly that you have to be very careful using them to explain to programmers.

    People are going to probe with follow-up Q's. They may know more about the car market than you, but assume you picked it because of those details, or may understand it in a funny way. Even if you agree completely on how cars work, the Q's won't be helpful. Are the Corolla CT1 and CX packages classes or instances? What about a 1994 Corrolla -- new class, or instance detail? What if we get non-dealer replacement parts? Are the Corrolla and Tercell instances of the common "CB4" design (several models are often made from a "secret" basic design). What happens when they stop making that type of car? Do lemons count as an instance? Different factories? The English left-side driver version?

    You have to quickly back up and say, sure, most analogies have many points in common, but everyone knows computer analogies are only ever good for a single rough idea.
     
  16. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,738
    Clearly the CT1 and CX packages are classes derived from the Corolla class. Model years are probably best represented as tags in the commit history of the project. Non-dealer replacement parts are extension methods. Lemons are improperly initialized instances.

    :p
     
    lordofduct likes this.