====== 098 セーフティーなAddComponent /GetComponent ====== ===== 概要 ===== よく使う機能は安全性の為にもラップしておくと良いと思います。 //------------------------------------------------------------- //! コンポーネントの追加(存在チェック版). //------------------------------------------------------------- static public T AddSafeComponent(GameObject _go) where T : Component { T component = _go.GetComponent(); if (component == null) { // 無ければ足す。 return _go.AddComponent(); } // あればそのまま返す。 return component; } //------------------------------------------------------------- //! コンポーネントの取得(存在チェック版). //------------------------------------------------------------- public static T GetSafeComponent(GameObject _go) where T : Component { T component = _go.GetComponent(); if(component == null) { Debug.LogError("Expected to find component of type " + typeof(T) + " but found none.", _go); } return component; }