- Home /
what is the difference between a function and a "class function"
what is the difference between a function and a "class function"?
Answer by fherbst · Jun 10, 2010 at 03:01 PM
Another meaning of "class function" could be static functions inside a class:
public class Tree : MonoBehaviour { private static int numberOfTreesCreated = 0;
public static int getNumberOfTreesCreated() { // If it wasn't just the treecount, you could do some calculations here return numberOfTreesCreated; }
void Start() { numberOfTreesCreated++; } }
You could then have a gameObject with the Tree script attached and would have access to
Tree.getNumberOfTreesCreated()
(with capital T, because its a static class method) from anywhere, which returns the total number of trees created. Maybe you meaned this?
Answer by Jeff Ciaccio · Jun 10, 2010 at 03:30 PM
While looking at the documention, I noticed two sections: functions and class functions.
So to use a "function", I need to instantiate and object and then allow that instance to call the function.
If it's a class function, it's a static function that can be called outside by simply using ClassName.classFunction. If there are variables updated in here, they must all be static too so there is only one copy, right?
Is this accurate?
Yes, this is accurate. $$anonymous$$y source code example above does exactly this. Inside Unity there are some class functions like GameObject.Find, which searches through all game objects in the scene. On the opposite, non-static functions are accessed through the instance of the object - for example gameObject.Find, which searches inside this particular GameObject and its childs.
Answer by newbrand · Jun 10, 2010 at 01:00 PM
Hm...
- A "class function" (better: method or member function) belongs to a class, a function doesn't.
- Methods are things a class can do while member variables are things it is. A dog for instance can Bark() (method) and has a variable itsAge (member variable).
- You can not call the method of a class, you must call the method of a class instance (object): Not Dog.Bark(), but var sparky: Dog = new Dog(); sparky.Bark(). See a class as a blueprint and an instance of it as the real thing.
In Unity, all functions are methods as all scripts are also classes/objects, so there's no difference at all.
Actually you can, if you declare the method as static, but that's maybe a bit too much for now...
Answer by dhendrix · Jun 10, 2010 at 12:57 PM
If your question is asking what I think you are asking then a class function can only be called through instances of that class. Your question is kind of vague though.
Your answer
