Unity How to Use Inheritance (Subclass)

  • Create a superclass
A.cs
using UnityEngine;
using System.Collections;
 
public class A : MonoBehaviour {
 
	public int number;
 
	public A() {
		Debug.Log("A constructor called.");
	}
}
  • Create a subclass
B.cs
using UnityEngine;
using System.Collections;
 
public class B : A {
 
	public B() {
		Debug.Log("B constructor called.");
 
		number = 1;
	}
}

You have access to public variables from the superclass. You can override them for example in the subclass constructor or Start() method.

Navigation
Print/export
Toolbox