Search Unity

Static Function?

Discussion in 'Scripting' started by leegod, Jun 22, 2010.

  1. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    What does static function and what is difference with normal function?
     
  2. Dreamora

    Dreamora

    Joined:
    Apr 5, 2008
    Posts:
    26,601
    static functions are class functions, not object functions (like static variables too)

    as such you access them through ClassName.StaticFunctionName

    as they are static and not related to a class instance, you can also only access static variables in them, not regular ones (as there is no object on which they are running)
     
    lauraaa and Kiwasi like this.
  3. leegod

    leegod

    Joined:
    May 5, 2010
    Posts:
    2,476
    Thanks but I don't understand...well..

    This is FPS tutorial's enemy Robot's script,

    Code (csharp):
    1.  
    2.  
    3. static function CopyTransformsRecurse (src : Transform,  dst : Transform) {
    4.     dst.position = src.position;
    5.     dst.rotation = src.rotation;
    6.    
    7.     for (var child : Transform in dst) {
    8.         // Match the transform with the same name
    9.         var curSrc = src.Find(child.name);
    10.         if (curSrc)
    11.             CopyTransformsRecurse(curSrc, child);
    12.     }
    13. }
    14.  
    Why here used static function?
     
  4. Landern

    Landern

    Joined:
    Dec 21, 2008
    Posts:
    354
    In the case of the FPS tutorial/script/demo thingy, as you can see the transform of both the source and destination objects are passed in the signature of the function(method). There would be no reason to instantiate this as part of an object because you have the objects in question pass through the sign. With that said, there are times when you want methods/functions/classes available in memory from the beginning of runtime. You create these as static(great for helper/utility classes) so you can execute their respective code because they are "typically" always available.

    Now let me give you a stupid example:

    Code (csharp):
    1.  
    2. // Static Class
    3. public static class MyClass
    4. {
    5.   static MyNameMethod(string myName)
    6.   {
    7.     Console.WriteLine(string.format("My Name Is: {0}", myName);
    8.   }
    9. }
    10.  
    11. //Non Static Class/Method
    12. public class MyClass
    13. {
    14.   MyNameMethod(string myName)
    15.   {
    16.     Console.WriteLine(string.format("My Name Is: {0}", myName);
    17.   }
    18. }
    19.  
    Now lets say you want to execute the MyNameMethod(function), using the static version you would do:

    Code (csharp):
    1.  
    2. MyClass.MyNameMethod("Jimmy James");
    3.  
    None static would be:

    Code (csharp):
    1.  
    2. MyClass myClassInstance = new MyClass();
    3. myClassInstance.MyNameMethod("Jimmy James Don Dawn");
    4.  
     
    et3rnald likes this.
  5. nikko

    nikko

    Joined:
    Mar 20, 2009
    Posts:
    436
    Static mean that you can access the data from anywhere (read global)

    The static function and variable have a global scope. You can access a static variable from any script. Variable that are not static are local to the current script file and cannot be accessed from another script.
     
    annasmags and Rocheofpower like this.
  6. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    This is not correct; static does NOT mean global (I have to emphasize this because it seems people get this wrong a lot). Dreamora already said what static means. If a variable is public, it is NOT local to that script, it can be accessed from anywhere (see this link which I've posted about 5 million times ;) ). Do NOT use static variables as a way of "making them global"; you are likely to create problems for yourself. (See the 5 million topics where people ask "how come when I change this variable for one enemy, it changes for them all?" Answer: 'Cause you made it static.)

    To be perfectly clear, static has nothing to do with global. It's quite possible to have a private static variable, in which case it's local ("private static var foo = 5;"). Public = global, private = local. Static = static (neither global nor local).

    --Eric
     
    SparkesRS, lauraaa and Naeim like this.
  7. Peter G

    Peter G

    Joined:
    Nov 21, 2009
    Posts:
    610
    Eric, that was beautiful. :D I don't know how many times I've told someone to remove the static keyword. I would have given a +1 on UA.

    We need to petition to have this page changed. Scroll down to "Global Variables" and start reading to see what I mean.
     
  8. sdgd

    sdgd

    Joined:
    Jan 15, 2013
    Posts:
    81
    heh I was just asking my self witch class member are you than changing

    thanks Eric5h5

    now I know
    private = local
    public = global

    static = global (but only 1 exists) am I right? well you say not private nor global
    private static = localized global (but only 1 exists)

    but if static limited with private to local it's localized global 1

    but well if everybody can access it than it's global but if not more than 1 exists than it's still global but only 1 even if class correct me if I'm wrong it's kinda late again 6AM and usually I'm fuzzy if I'm tired.

    I'm asking my self why wouldn't it be global as it's same

    static == public static

    please correct me I want to know more about it


    and at space wisdom what takes less?

    I'm asking because static means only 1 and constant could mean more than 1
    and let say example in class MonoBehaviour

    const
    static const

    and what's the difference between
    static class
    static function

    I see no difference as class is made for 1000 same classes if needed (with different inside)
    and function is always same inside as only 1 exists (except arrays)

    class and function can contain more functions
    and than static calss doesn't need constructor if only 1 exists
     
    Last edited: Jan 27, 2013
  9. Cascho01

    Cascho01

    Joined:
    Mar 19, 2010
    Posts:
    1,347
    I want to learn how to use static functions but don´t know how they work:

    Lets say I have some boxes, they all share the same script with a boolean named "selected".
    If I click on a box its boolean gets true, while others booleans get false by using a static function.

    My script (remember its on all boxes) would look like:

    I don´t know how to call the static function on all boxes and also I can´t put the boolean into that function since its not static.

    Please help me (Javascript please) - thanks!
     
    Last edited: Feb 14, 2014
  10. nevaran

    nevaran

    Joined:
    May 21, 2010
    Posts:
    247
    Anyone know if you can use static functions to manipulate the current object that the script is attached to?

    Right now the only way i know to do it is by making a static variable reference, for example:

    myScript.control.BlaFunction
     
  11. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    No, you can't. Fundamentally, a static function is not attached to anything. The script might be attached to one object, to a hundred objects, or it might not be in the scene at all - and none of that matters a bit to a static function, because it's not attached to any of them.

    If you want an exception, search for information the singleton pattern. There's a ton of discussion on how to do it and whether or not it should be done, that I couldn't do justice to here.

    If you're not using a singleton, then merely asking that question suggests that the function should probably not be static at all.