覚えること
サンプルコード1
オーバーロードしたコンストラクタだけが呼ばれる。引数なしコンストラクタは自動で呼ばれない。(継承の場合と違うので注意)
public class Test { public Test() { Debug.Log(1); } public Test(int x) { Debug.Log(x); } }
public class Main : MonoBehaviour { void Start() { Test test = new Test(2); } }
MainをGameObjectにはりつける
結果 2
サンプルコード2
必ず実行させたい処理があるならば、this()でコンストラクタを継承させる
public class Test { public Test() { Debug.Log(1); } public Test(int x) : this() { Debug.Log(x); } }
public class Main : MonoBehaviour { void Start() { Test test = new Test(2); } }
MainをGameObjectにはりつける
結果 1 2