Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Short Question About Instantiate.

Discussion in 'Scripting' started by psykick1, Jun 24, 2020.

  1. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Hello ,

    Instantiate is a static method inside Object class.
    How can we call it from our script?
    As I know in C# static members cannot inherit.

    Thank you.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,890
    Instantiate is a public static method. Therefore from any script you can call:
    Code (CSharp):
    1. Object.Instantiate(..)
    In MonoBehaviours, you don't need the "Object" prefix because MonoBehaviour extends from Object (via Behaviour -> Component -> Object). Therefore Instantiate is part of MonoBehaviour, and you don't need to use the class prefix to call static members of your own class.
     
    Madgvox likes this.
  3. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    They do not need to be inherited to be called. Instantiate is a public method, and therefore is accessible from everywhere as
    Object.Instantiate
    .

    In MonoBehaviours, which inherit from
    UnityEngine.Object
    , the
    Object.
    prefix can be dropped, as static members are implicitly accessible from child classes, meaning that you can just call
    Instantiate
    .
     
    PraetorBlue likes this.
  4. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    I know the inheritence hirerchy
    MyScript >> Behaviour >> MonoBehaviour >> Component >> Object.

    What what I didnt knew is that static implicity accessible from child classes.
    Thanks for answers.
     
  5. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    You swapped MonoBehaviour and Behaviour. For the sake of completeness, it should be:

    MyScript >> MonoBehaviour >> Behaviour >> Component >> Object
     
    PraetorBlue likes this.
  6. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Haha...
    I just tried it.. and its work .

    I made an Class with a public static method
    Call it from another class that inherit this class.
    And then , Instantiate this class... and its f[ollowed me to the next thinking:

    Instantiate cloning the GameObject including all the component attached to him + also our script.
    If in our script there is a Instantiated method and we clone it, so it means we also clone the instantiate method..
    How does Instantied keep cloning only 1 clone?
     
  7. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Static methods are not associated with any instance of a class, and creating a new instance of the class does not create a new instance of the Instantiate method.
     
    Vryken likes this.
  8. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Its not make sense becuse,
    When we instantiate a Class that Say (for example) Vector3.Distance , the Vector3.Distance will work for every instance of the class.
     
  9. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Exactly. Static methods aren't associated with any instance of a class, and so are available to be used anywhere.
     
    Vryken likes this.
  10. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Hi,

    It means that when you instantiate "MyScript"
    the static method will called once from each script.
    Means the object will instance X2 and X3 becuse there is more scripts that call this method.

    I Belive that unity said something like this

    if (Original) {
    Instantiate.
    }
     
  11. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    I'm not following what your question/concern is. Do you have a snippet of code that isn't working? If so, please post it.
     
  12. psykick1

    psykick1

    Joined:
    May 19, 2020
    Posts:
    61
    Hi ,

    If we instantiated our script that is component of game object,
    It means that we instantiated a script that call the instantiate .
    so instantiate will call twice.. and then 4 time and then 8 time and etc..

    I solved this problem with Singleton pattern.
     
    Last edited: Jul 2, 2020