0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

[自分用メモ]gameObjectとGameObject、thisについて

Last updated at Posted at 2018-12-09

自分用メモ

・gameObjectの場合
 このスクリプトがアタッチされているゲームオブジェクトを参照する

・GameObjectの場合
 GameObjectという型宣言?になるため、アタッチされているゲームオブジェクトは参照しない

・this
 スクリプトのインスタンスを指している
ゲームオブジェクトにアタッチされている自分自身のスクリプトのクラスを見ていることになる
インスタンスを指しているので、静的(static)なものには使用できない

・this.gameObject
 このスクリプトがアタッチされているゲームオブジェクトを参照する
「gameObject」と同じ

・this.Getcomponent< Transform >()

 このスクリプトこのインスタンス化された自分自身のクラスを参照しているけど
MonoBehaviourを継承している場合はGetComponentを使用できる

ゲームオブジェクトにコンポーネントされているtransformなどを取得する場合は
this.gameObject.Getcomponent< Transform >();
として使用する必要がある

this.GetComponent< Transform >();
とすると、MonoBehaviourを継承している場合は使用できるが
MonoBehaviourを継承していない場合は、
this(このクラス)が継承しているMonoBehaviourのGetComponentを参照しようとするので
エラーになる

参考にしたサイト
https://qiita.com/megane42/items/9709d696e8f2561dbb1d
http://pafu-of-duck.hatenablog.com/entry/2017/05/17/213751

0
2
0

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
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?