LoginSignup
10
8

More than 5 years have passed since last update.

GetComponentの呼び出し方

Posted at

GetComponentの呼び出し方に何通りかあり、どのように使うか調べてみました。
↓こちらに詳しく書かれていました。
http://blog.be-style.jpn.com/article/57118009.html

GetComponent
// すべてのGameObjectの中から指定したコンポーネントを検索する.
GameObject.GetComponent<コンポーネント名>()
gameObject.GetComponent<コンポーネント名>()

// gameObjectが持っているコンポーネントから検索する.
gameObject.transform.GetComponent<コンポーネント名>()

transform自体はすべてのGameObjectが必須で持っているコンポーネントになります。
transformから見た兄弟コンポーネントを検索することで、検索する範囲が狭くなるということですね。
(感覚的には2番目のgameObjectも自身が持っているコンポーネントを検索しそうなものですが・・・)

10
8
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
8