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

How to create and call a static (global) function?

Discussion in 'Scripting' started by asterix, Aug 25, 2009.

  1. asterix

    asterix

    Joined:
    Aug 1, 2009
    Posts:
    245
    How to create and call a static (global) function?
     
  2. HanulTech

    HanulTech

    Joined:
    Apr 5, 2009
    Posts:
    312
    Assuming you mean in Javascript, then like this:

    In a file called, for example, Global.js:

    Code (csharp):
    1. public static function DoFoo() {
    2.  ....
    3. }
    4.  
    5. public static function DoFoo(): int {
    6.  ....
    7. }
    8.  
    9. public static function DoFoo(someValue: int): int {
    10.  ....
    11. }
    12.  
    etc....

    Functions can then be called in any other scripts like this:

    Code (csharp):
    1. Global.DoFoo();
    2.  
    3. var x: int = Global.DoFoo();
    4.  
    5. var y: int = Global.DoFoo(12);
    etc....
     
  3. asterix

    asterix

    Joined:
    Aug 1, 2009
    Posts:
    245
    :D

    Thanks