Static vs Non-static functions and variables

I am quite new to this programming scene and try to figure out from examples but some things bother me about static and non-static things that I can’t find answer online.

  1. Should I reference object in script and call public functions or just set them static and call them static way (wich is faster)
  2. Why cant I comminacte normal functions and static functions and when to use static functions and variables

I tried to search thoese answers on google and either i dont understand them or can’t fight good explanations. Any kind of help would be good.


I think you should read a guide about object oriented programming in general. To understand all those concepts of classes/instances, static/non-static, inheritence and polymorphism is really worth the effort.
But I will try to give you an example (that is not related to game programming on purpose):

Let’s consider a class representing an emperor penguin:
(Be aware that I might violate naming conventions, as C# is a new language for me, too)

class EmperorPenguin {
	static string conservationStatus = "Near Threatened";
	float weight = 15;
	static bool doesItLike(Food f) {
		return f.isFish();
	void eat(Food f) {
		if(EmperorPenguin.doesItLike(f)) {
			weight += f.getWeight();

The variable conservationStatus is static, because it is a property of the species. Not one emperor penguin is Near Threatened but the “community” of emperor penguins. Weight is an instance variable because it is per individual: Two penguins might not share the same weight.

For the functions it’s the same: All penguins eat the same food (without respect to personal preferrences like in human beings), so the method that checks whether a penguin likes a particular piece of food can be static. Eat() should increase the weight of the specific penguin eating the food, so this method must not be static.

You can see that the non-static method can call the static one, as it can be accessed from everywhere by the class-name. But the static function may not call instance methods or variables, as it belongs to all penguins, not to a single individual.

To call those non-static methods, you need to create an instance: Taking the “construction plan” (the class) and create a concrete object from it:

EmperorPenguin p = new EmperorPenguin();
Food f = new Food("fish", 0.5f);;
//You can create a second penguin with different properties:
EmperorPenguin p2 = new EmperorPenguin();
Food f = new Food("fish", 0.75f);;
//Possible without any instance: