Differences between functions

hello world

I think I have some very basic questions

what is the differences between this functions and

what are the best for the performance of the iPhone ?

function

private function

static function

private static function

thank you

function is a public member function which requires an object and can be called externally from the object - ObjectName.functionName(). The variable “this” used within the function refers to the object.

private function is a private member function that can be called from within the object, or in some languages from “friends”.

static function is a public class function which can be called globally using the name of class, like this “ClassName.function()”, and does not have an object associated with it.

private static function is a private class function callable from within other functions in the same class (static or member) and does not have an object associated with it.

Member functions can reference both member variables of the object and static variables of the class.

In a static function, code can reference static variables of the class, but not member variables because there is no object.

There’s no difference in performance, other than member functions have a “this” object pointer which behind the scenes is passed as a hidden first parameter.

thanks for the quick response :slight_smile:

Sorry to dig this up again, but this is all still quite confusing for me… :?

By
“requires an object”
do you mean
“the script in which this function is called requires a Unity’s ‘GameObject’”?

If so, how can a static function not require one?
How can a static function be part of the Scene if it’s not attatched to a GameObject? How can it ever be called?

Do I have the right understanding that instanciated copies of the script somehow share the static variables, while the member variables are unique to the instance?

So for example I have two robots im my scene with the same script Positions.js attatched.

// Positions.js: 

var memberPosition : Vector3;
static var staticPositionCalledInMember : Vector3;
static var staticPostion : Vector3;

function RobotPosition ()
{
  memberPosition = transform.position;
  staticPositionCalledInMember = transform.position;
}

static function StaticRobotPosition ()
{
  staticPosition = transform.position;
}

function Update ()
{
  RobotPosition ();
  StaticRobotPosition ();
}

What would happen? I’m guessing:

The memberPosition-Variable in the Script of Robot1 would store the Position of Robot1.
The memberPosition-Variable in the Script of Robot2 would store the Position of Robot2.

If I understand this right, staticPosition-Variable would not store anything, because as it’s called from a static function it doesn’t know who or what it should take the position of, as it doesn’t know if/that it’s attatched to a GameObject??

As for staticPositionCalledInMember… wouldn’t that store the Position of Robot1, just to be overwritten in the same frame with the Position of Robot2 (or vice versa, depending on pure coincidence?)

Doesn’t seem all too useful at first glance :?
But maybe for things like that:
Say I have some Lamps in the room and once 3 are switched on something happens… could I go like that:

static var lightsOn : int = 0;

function OnTriggerEnter (other: Collider)
{
  lightsOn ++;
}

static function ThreeLightsOn ()
{
  if (lightsOn => 3)
   {
      do something here...
   }
}

function Update ()
{
  ThreeLightsOn ();
}

Now if I hit the trigger of any of the Lamps, would the lightsOn-Variable get updated for all the lamps?

And would the static function ThreeLightsOn only be called once per frame for the whole scene, even though it’s instanciated in several GameObjects?

Or would it be called for each Instance, as it’s called in the Update-Function?

If the former case is true, how could this not result in some performance-boost?

If the later case is true, wouldn’t the “do something here…” be performed several times? Oo
And in that case… what’s the benefit of the static function? Only that it’s more convenient to call?

Also, that static functions are more convenient to call from other Objects - isn’t that a major performance boost, too, compared to the slow GameObject.Find? As I thought ObjectName.functionName() won’t work?

(Btw, can you call whole classes without specifying single functions within them?)

Sorry for the long rant… I think this already disentangled a few knots in my brain, but as you see, still a lot of confusion around…
Please enlighten me! =D

Greetz, Ky.

Static members (ie, variables or functions) are members that are associated with the class itself rather than any given instance of it. Each class declaration only exists once in the system, but there can be many instance objects created from that class. Although these members are declared with the keyword “static”, they are also known as class variables and class functions.

A normal, non-static function can access static and non-static variables in more or less the same way, but the values of the static ones are shared between all instances of the class - when a value is changed, all instances see the new value.

A static function, by contrast, can only access static variables. This is because the function can be called even when no instance of the class exists.

Static functions are useful as “factory” functions, ie, functions that create new instances of the class in a special way that goes beyond what the constructor does. An example of this idea can be found in the Quaternion class. Several of the Quaternion class functions just create a new Quaternion, with the values set up in a particular way. An example of a use for static variables is in creating unique object ID numbers. The constructor, or a factory method, can store the next available ID number in a static variable and increment it after it is taken.