質問編集履歴

1

書式変更

2022/06/22 22:04

投稿

pra
pra

スコア17

test CHANGED
File without changes
test CHANGED
@@ -1,197 +1,101 @@
1
1
  Unityで2Dシューティングゲームを作りたいと思い、数日前からサイト(http://denshikousaku.net/unity3d-2d-shooting-part2)でUnityでのシューティングゲーム制作のチュートリアルを始めたものです。
2
2
 
3
-
4
-
5
- ###発生している問題・エラーメッセージ
3
+ ### 発生している問題・エラーメッセージ
6
-
7
-
8
4
 
9
5
  このサイトの第3回まで終えて第4回目を行っていた時のことです。プレイヤーや敵の弾が自動で消えるような文をスクリプトに書いてゲームを再生したとき、
10
-
11
6
  "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it."のエラーが出て、プレイヤーの弾が放出された後弾がその場にとどまって数秒後に弾が放出されなくなってしまいました。敵とその弾は正常に動いているように見えました。このエラーは以下の「プレイヤーに関するスクリプト」に出ました。
12
7
 
8
+ ### 該当のソースコード
9
+ ```C#
10
+ //弾に関するスクリプト
11
+ using System.Collections;
12
+ using System.Collections.Generic;
13
+ using UnityEngine;
13
14
 
15
+ public class Bullet : MonoBehaviour
16
+ {
17
+ public int speed = 10;
18
+ public float lifeTime = 5;
14
19
 
15
- ###該当のソースコード
20
+ void Start ()
21
+ {
22
+ GetComponent<Rigidbody2D>().velocity = transform.up.normalized * speed;
23
+ Destroy(gameObject, lifeTime);
24
+ }
25
+ }
16
26
 
17
- //に関するスクリプト
27
+ //プレイヤーに関するスクリプト
28
+ using System.Collections;
29
+ using System.Collections.Generic;
30
+ using UnityEngine;
18
31
 
32
+ public class Player : MonoBehaviour {
33
+ Spaceship spaceship;
34
+ IEnumerator Start ()
35
+ {
36
+ spaceship = GetComponent<Spaceship>();
37
+ while (true)
38
+ {
39
+ spaceship.Shot(transform);
40
+ yield return new WaitForSeconds (spaceship.shotDelay);
41
+ }
42
+ }
43
+
44
+ private void Update ()
45
+ {
46
+ float x = Input.GetAxisRaw("Horizontal");
47
+ float y = Input.GetAxisRaw("Vertical");
48
+ Vector2 direction = new Vector2(x, y).normalized;
49
+ spaceship.Move(direction);
50
+ }
51
+
52
+ private void OnTriggerEnter2D(Collider2D c)
53
+ {
54
+ string layerName = LayerMask.LayerToName(c.gameObject.layer);
55
+ if (layerName == "Enemy")
56
+ {
57
+ Destroy(c.gameObject);
58
+ }
59
+ if (layerName == "EnemyBullet" || layerName == "Enemy")
60
+ {
61
+ Destroy(gameObject);
62
+ }
63
+ }
64
+ }
65
+
66
+ //プレイヤー・敵に共通のスクリプト
19
67
  using System.Collections;
20
-
21
68
  using System.Collections.Generic;
22
-
23
69
  using UnityEngine;
24
70
 
25
71
 
26
-
72
+ [RequireComponent(typeof(Rigidbody2D))]
27
- public class Bullet : MonoBehaviour
73
+ public class Spaceship : MonoBehaviour {
28
-
29
- {
30
-
31
- public int speed = 10;
74
+ public float speed;
32
-
33
- public float lifeTime = 5;
75
+ public float shotDelay;
34
-
76
+ public GameObject bullet;
35
-
36
-
37
- void Start ()
77
+ public void Shot (Transform origin)
38
-
39
78
  {
40
-
41
- GetComponent<Rigidbody2D>().velocity = transform.up.normalized * speed;
79
+ Instantiate(bullet, origin.position, origin.rotation);
42
-
43
- Destroy(gameObject, lifeTime);
44
-
45
80
  }
46
81
 
82
+ public void Move(Vector2 direction)
83
+ {
84
+ GetComponent<Rigidbody2D>().velocity = direction * speed;
85
+ }
47
86
  }
48
87
 
88
+ ```
49
89
 
50
-
51
- //プレイヤーに関するスクリプト
52
-
53
- using System.Collections;
54
-
55
- using System.Collections.Generic;
56
-
57
- using UnityEngine;
58
-
59
-
60
-
61
- public class Player : MonoBehaviour {
62
-
63
- Spaceship spaceship;
64
-
65
- IEnumerator Start ()
66
-
67
- {
68
-
69
- spaceship = GetComponent<Spaceship>();
70
-
71
- while (true)
72
-
73
- {
74
-
75
- spaceship.Shot(transform);
76
-
77
- yield return new WaitForSeconds (spaceship.shotDelay);
78
-
79
- }
80
-
81
- }
82
-
83
-
84
-
85
- private void Update ()
86
-
87
- {
88
-
89
- float x = Input.GetAxisRaw("Horizontal");
90
-
91
- float y = Input.GetAxisRaw("Vertical");
92
-
93
- Vector2 direction = new Vector2(x, y).normalized;
94
-
95
- spaceship.Move(direction);
96
-
97
- }
98
-
99
-
100
-
101
- private void OnTriggerEnter2D(Collider2D c)
102
-
103
- {
104
-
105
- string layerName = LayerMask.LayerToName(c.gameObject.layer);
106
-
107
- if (layerName == "Enemy")
108
-
109
- {
110
-
111
- Destroy(c.gameObject);
112
-
113
- }
114
-
115
- if (layerName == "EnemyBullet" || layerName == "Enemy")
116
-
117
- {
118
-
119
- Destroy(gameObject);
120
-
121
- }
122
-
123
- }
124
-
125
- }
126
-
127
-
128
-
129
- //プレイヤー・敵に共通のスクリプト
130
-
131
- using System.Collections;
132
-
133
- using System.Collections.Generic;
134
-
135
- using UnityEngine;
136
-
137
-
138
-
139
-
140
-
141
- [RequireComponent(typeof(Rigidbody2D))]
142
-
143
- public class Spaceship : MonoBehaviour {
144
-
145
- public float speed;
146
-
147
- public float shotDelay;
148
-
149
- public GameObject bullet;
150
-
151
- public void Shot (Transform origin)
152
-
153
- {
154
-
155
- Instantiate(bullet, origin.position, origin.rotation);
156
-
157
- }
158
-
159
-
160
-
161
- public void Move(Vector2 direction)
162
-
163
- {
164
-
165
- GetComponent<Rigidbody2D>().velocity = direction * speed;
166
-
167
- }
168
-
169
- }
170
-
171
-
172
-
173
-
174
-
175
- ###試したこと
90
+ ### 試したこと
176
-
177
91
  このエラーについてネットで検索して、弾が消された後にスクリプトで弾が存在することを要求してしまっているのでエラーが起きていのではないかと思いました。しかし、それをどうやったら解消できるのかわかりません。
178
92
 
179
93
 
180
-
181
-
182
-
183
- ###前提・実現したいこと
94
+ ### 前提・実現したいこと
184
-
185
95
  "MissingReferenceException"をなくして、プレイヤーから正常に弾が放出されるようにしたいです。
186
96
 
187
97
 
188
-
189
-
190
-
191
- ###補足情報(言語/FW/ツール等のバージョンなど)
98
+ ### 補足情報(言語/FW/ツール等のバージョンなど)
192
-
193
99
  サイトのチュートリアルではプレイヤーや敵が被弾した際に爆発するスクリプト・画像を導入しているのですが、私は画像を自分で描いていて、爆発の絵を描いていないためスクリプトの爆発に関した部分を省略しています。その省略で必要な命令が欠けてエラーが起きてしまったのかもしれないと思いました(サイトのお手本見比べましたが自分ではわからなかったです)。
194
-
195
100
  使用言語はC♯、Unityはver.5.5.1です。
196
-
197
101
  よろしくお願いします。