what is the difference between a function and a "class function"

what is the difference between a function and a "class function"?

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...

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?

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.

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?