Class Constructor

Learn about constructors in C#. Understand how to initialize objects and use constructor overloading.

What is a Constructor?

A constructor is a special function that automatically executes when creating an instance (object) of a class. It initializes the object.

Default Constructor

A constructor with no parameters. Automatically created if you don't write one.

Default Constructor Example

public class Weapon
{
    public string name;
    public int damage;
    
    // Default constructor
    public Weapon()
    {
        name = "Unknown";
        damage = 10;
    }
}

// Usage
Weapon weapon = new Weapon();  // Calls default constructor
// weapon.name = "Unknown", weapon.damage = 10

Constructor with Parameters

Can initialize by passing values when creating.

Parameter Constructor Example

public class Weapon
{
    public string name;
    public int damage;
    
    // Constructor with parameters
    public Weapon(string name, int damage)
    {
        this.name = name;      // this refers to current object
        this.damage = damage;
    }
}

// Usage
Weapon sword = new Weapon("Sword", 50);
// sword.name = "Sword", sword.damage = 50

this Keyword

  • this refers to the current object
  • Used to distinguish when parameter name and variable name are the same
public Weapon(string name, int damage)
{
    this.name = name;        // this.name = class variable
    this.damage = damage;    // name = parameter
}

Multiple Constructors (Constructor Overloading)

You can create multiple constructors in the same class. Parameter counts or types must be different.

Constructor Overloading Example

public class Weapon
{
    public string name;
    public int damage;
    
    // Default constructor
    public Weapon()
    {
        name = "Unknown";
        damage = 10;
    }
    
    // Constructor with parameters
    public Weapon(string name, int damage)
    {
        this.name = name;
        this.damage = damage;
    }
    
    // Constructor that only takes name
    public Weapon(string name)
    {
        this.name = name;
        this.damage = 20;  // Default damage
    }
}

// Usage
Weapon weapon1 = new Weapon();              // Default constructor
Weapon weapon2 = new Weapon("Sword", 50);      // Parameter constructor
Weapon weapon3 = new Weapon("Shield");        // Name-only constructor

Unity MonoBehaviour and Constructors

When using classes that inherit from Unity's MonoBehaviour, be careful when using constructors.

Notes

public class script1123_04 : MonoBehaviour
{
    public Weapon weapon;
    
    // Constructors are not recommended in MonoBehaviour
    // It's better to use Unity lifecycle functions (Awake, Start)
}

Reason: Unity controls how objects are created, so using Awake() or Start() is safer than constructors.

Unity Function Execution Order

Unity calls functions in a specific order when scripts execute.

Execution Order

  1. Awake() - When object is created (right after constructor)
  2. OnEnable() - When object is enabled
  3. Start() - Once before first frame
  4. Update() - Every frame
  5. LateUpdate() - After Update
  6. OnDisable() - When object is disabled
  7. OnDestroy() - When object is destroyed

Detailed Information

Unity Official Documentation: Execution Order

Example

public class Player : MonoBehaviour
{
    void Awake()
    {
        Debug.Log("1. Awake executed");
    }
    
    void Start()
    {
        Debug.Log("2. Start executed");
    }
    
    void Update()
    {
        Debug.Log("3. Update executed (every frame)");
    }
}

Constructor Chaining

One constructor can call another constructor.

public class Weapon
{
    public string name;
    public int damage;
    
    // Default constructor
    public Weapon() : this("Unknown", 10)
    {
        // Calls above constructor to initialize
    }
    
    // Parameter constructor
    public Weapon(string name, int damage)
    {
        this.name = name;
        this.damage = damage;
    }
}

Practical Usage Examples

Example 1: Weapon Class

public class Weapon
{
    public string name;
    public int damage;
    
    // Default constructor
    public Weapon()
    {
        name = "Unknown";
        damage = 10;
    }
    
    // Parameter constructor
    public Weapon(string name, int damage)
    {
        this.name = name;
        this.damage = damage;
    }
}

// Usage
Weapon sword = new Weapon("Sword", 50);
Weapon shield = new Weapon("Shield", 20);
Weapon defaultWeapon = new Weapon();  // Use default values

Example 2: Player Class

public class Player
{
    public string name;
    public int level;
    public int hp;
    
    public Player()
    {
        name = "Player";
        level = 1;
        hp = 100;
    }
    
    public Player(string name, int level)
    {
        this.name = name;
        this.level = level;
        this.hp = 100;  // Default HP
    }
}

Constructor vs Unity Lifecycle

Method When to Use Features
Constructor Regular classes Initialize when object is created
Awake() MonoBehaviour Initialize Unity objects
Start() MonoBehaviour Initialize before first frame

Notes

  1. Avoid constructors in MonoBehaviour: Use Unity lifecycle for safety
  2. Constructor name must match class name
  3. No return type (not even void)
  4. Multiple constructors must have different parameters

← Back to Table of Contents