質問するログイン新規登録

質問編集履歴

1

追記

2017/02/01 14:34

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -33,4 +33,44 @@
33
33
  Animator animeData = this.GetComponent<Animator> ();
34
34
  Animator animeData = this.gameObject.GetComponent<Animator> ();
35
35
  ```
36
- thisとthis.gameObjectの違いや、使い分けのご教授をお願いします。
36
+ thisとthis.gameObjectの違いや、使い分けのご教授をお願いします。
37
+
38
+
39
+ ###追記
40
+
41
+ ゲーム再生時にNullエラーが起きるのを検証しようとしました。
42
+
43
+ まず、ゲームオブジェクトにアタッチしているスクリプトを下記のようにしました。
44
+ ```C#
45
+ using UnityEngine;
46
+ using System.Collections;
47
+
48
+ public class Sample: MonoBehaviour{
49
+
50
+ void Start () {
51
+ Hoge hoge = new Hoge ();
52
+ }
53
+ }
54
+ ```
55
+
56
+ 次にゲームオブジェクトにアタッチさせないスクリプトも、MonoBehaviourを継承させて、下記のようにしました。
57
+ ```C#
58
+ using UnityEngine;
59
+ using System.Collections;
60
+
61
+ public class Hoge: MonoBehaviour{
62
+
63
+ void Start () {
64
+ Debug.Log(this.gameObject);
65
+ }
66
+ }
67
+ ```
68
+
69
+ ゲーム再生時に発生したエラー(警告文)。
70
+ 「MonoBehaviourを継承したクラスはnewできない」のでしょうか?
71
+ ```
72
+ You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
73
+ UnityEngine.MonoBehaviour:.ctor()
74
+ Hoge:.ctor()
75
+ Sample:Start() (at Assets/Sample.cs:7)
76
+ ```