Recently I started messing with unity, began to look at examples of scripts and came across such a construction:

public class MakeFX : MonoBehaviour { private static MakeFX instance; public static MakeFX Instance { get { if (instance == null) instance = GameObject.FindObjectOfType<MakeFX>(); return instance; } } ... } 

After C ++, this construction looks strange to me. Googling help in msdn did not get any better. Explain what it is and why it is needed.

  • one
    This property. Read any starting book on C #. - Vlad from Moscow
  • one
    An instance is an object instance. In this case, the Singleton pattern is implemented, which allows you to create only one instance of the specified type for the entire application. - qzavyer
  • @qzavyer I would recommend you to issue your comment as an answer, because it is the most understandable of all :) And also, could you give an example of how to use it without Singleton, i.e. so that I can create more than one object using an instance, if it is implementable. - Shadr

2 answers 2

From the point of view of language, C # is the most common property name. From the English language, this word is translated as "instance".

Most often, such a static property can be found in the implementation of the Singleton design pattern (pattern).

I note that the code you provide is not a canonical implementation, since a single instance of a class is created by someone from the outside, not from the inside.

  • Why is the canonical implementation being created outside? Always created (C ++) static method GetInstance with a static local class object and returned a reference to it. What is the profit of creation from the outside? - int3
  • @ int3 is obvious, in this case, an instance of the class is "thrown" on the scene in a visual editor or something like that. Why do so - I do not know. - Pavel Mayorov

This is simply the name of the property, in this case translated as "instance". It’s just so called to call some static fields, especially when implementing the синглтон pattern.