**以前のリビジョンの文書です**
098 セーフティーなAddComponent /GetComponent
//-------------------------------------------------------------
//! コンポーネントの追加(存在チェック版).
//-------------------------------------------------------------
static public T AddSafeComponent<T>(GameObject _go) where T : Component
{
T component = _go.GetComponent<T>();
if (component == null)
{
// 無ければ足す。
return _go.AddComponent<T>();
}
// あればそのまま返す。
return component;
}
//-------------------------------------------------------------
//! コンポーネントの取得(存在チェック版).
//-------------------------------------------------------------
public static T GetSafeComponent<T>(GameObject _go) where T : Component
{
T component = _go.GetComponent<T>();
if(component == null)
{
Debug.LogError("Expected to find component of type " + typeof(T) + " but found none.", _go);
}
return component;
}