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

質問編集履歴

1

書式変更

2022/06/22 22:04

投稿

pra
pra

スコア17

title CHANGED
File without changes
body CHANGED
@@ -1,99 +1,101 @@
1
- Unityで2Dシューティングゲームを作りたいと思い、数日前からサイト(http://denshikousaku.net/unity3d-2d-shooting-part2)でUnityでのシューティングゲーム制作のチュートリアルを始めたものです。
2
-
3
- ###発生している問題・エラーメッセージ
4
-
5
- このサイトの第3回まで終えて第4回目を行っていた時のことです。プレイヤーや敵の弾が自動で消えるような文をスクリプトに書いてゲームを再生したとき、
6
- "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it."のエラーが出て、プレイヤーの弾が放出された後弾がその場にとどまって数秒後に弾が放出されなくなってしまいました。敵とその弾は正常に動いているように見えました。このエラーは以下の「プレイヤーに関するスクリプト」に出ました。
7
-
8
- ###該当のソースコード
9
- //弾に関するスクリプト
10
- using System.Collections;
11
- using System.Collections.Generic;
12
- using UnityEngine;
13
-
14
- public class Bullet : MonoBehaviour
15
- {
16
- public int speed = 10;
17
- public float lifeTime = 5;
18
-
19
- void Start ()
20
- {
21
- GetComponent<Rigidbody2D>().velocity = transform.up.normalized * speed;
22
- Destroy(gameObject, lifeTime);
23
- }
24
- }
25
-
26
- //プレイヤーに関するスクリプト
27
- using System.Collections;
28
- using System.Collections.Generic;
29
- using UnityEngine;
30
-
31
- public class Player : MonoBehaviour {
32
- Spaceship spaceship;
33
- IEnumerator Start ()
34
- {
35
- spaceship = GetComponent<Spaceship>();
36
- while (true)
37
- {
38
- spaceship.Shot(transform);
39
- yield return new WaitForSeconds (spaceship.shotDelay);
40
- }
41
- }
42
-
43
- private void Update ()
44
- {
45
- float x = Input.GetAxisRaw("Horizontal");
46
- float y = Input.GetAxisRaw("Vertical");
47
- Vector2 direction = new Vector2(x, y).normalized;
48
- spaceship.Move(direction);
49
- }
50
-
51
- private void OnTriggerEnter2D(Collider2D c)
52
- {
53
- string layerName = LayerMask.LayerToName(c.gameObject.layer);
54
- if (layerName == "Enemy")
55
- {
56
- Destroy(c.gameObject);
57
- }
58
- if (layerName == "EnemyBullet" || layerName == "Enemy")
59
- {
60
- Destroy(gameObject);
61
- }
62
- }
63
- }
64
-
65
- //プレイヤー・敵に共通のスクリプト
66
- using System.Collections;
67
- using System.Collections.Generic;
68
- using UnityEngine;
69
-
70
-
71
- [RequireComponent(typeof(Rigidbody2D))]
72
- public class Spaceship : MonoBehaviour {
73
- public float speed;
74
- public float shotDelay;
75
- public GameObject bullet;
76
- public void Shot (Transform origin)
77
- {
78
- Instantiate(bullet, origin.position, origin.rotation);
79
- }
80
-
81
- public void Move(Vector2 direction)
82
- {
83
- GetComponent<Rigidbody2D>().velocity = direction * speed;
84
- }
85
- }
86
-
87
-
88
- ###試したこと
89
- このエラーについてネットで検索して、弾が消された後にスクリプトで弾が存在することを要求してしまっているのでエラーが起きていのではないかと思いました。しかし、それをどうやったら解消できるのかわかりません。
90
-
91
-
92
- ###前提・実現したいこと
93
- "MissingReferenceException"をなくして、プレイヤーから正常に弾が放出されるようにしたいです。
94
-
95
-
96
- ###補足情報(言語/FW/ツール等のバージョンなど)
97
- サイトのチュートリアルではプレイヤーや敵が被弾した際に爆発するスクリプト・画像を導入しているのですが、私は画像を自分で描いていて、爆発の絵を描いていないためスクリプトの爆発に関した部分を省略しています。その省略で必要な命令が欠けてエラーが起きてしまったのかもしれないと思いました(サイトのお手本見比べましたが自分ではわからなかったです)。
98
- 使用言語はC♯、Unityはver.5.5.1です。
1
+ Unityで2Dシューティングゲームを作りたいと思い、数日前からサイト(http://denshikousaku.net/unity3d-2d-shooting-part2)でUnityでのシューティングゲーム制作のチュートリアルを始めたものです。
2
+
3
+ ### 発生している問題・エラーメッセージ
4
+
5
+ このサイトの第3回まで終えて第4回目を行っていた時のことです。プレイヤーや敵の弾が自動で消えるような文をスクリプトに書いてゲームを再生したとき、
6
+ "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it."のエラーが出て、プレイヤーの弾が放出された後弾がその場にとどまって数秒後に弾が放出されなくなってしまいました。敵とその弾は正常に動いているように見えました。このエラーは以下の「プレイヤーに関するスクリプト」に出ました。
7
+
8
+ ### 該当のソースコード
9
+ ```C#
10
+ //弾に関するスクリプト
11
+ using System.Collections;
12
+ using System.Collections.Generic;
13
+ using UnityEngine;
14
+
15
+ public class Bullet : MonoBehaviour
16
+ {
17
+ public int speed = 10;
18
+ public float lifeTime = 5;
19
+
20
+ void Start ()
21
+ {
22
+ GetComponent<Rigidbody2D>().velocity = transform.up.normalized * speed;
23
+ Destroy(gameObject, lifeTime);
24
+ }
25
+ }
26
+
27
+ //プレイヤーに関するスクリプト
28
+ using System.Collections;
29
+ using System.Collections.Generic;
30
+ using UnityEngine;
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
+ //プレイヤー・敵に共通のスクリプト
67
+ using System.Collections;
68
+ using System.Collections.Generic;
69
+ using UnityEngine;
70
+
71
+
72
+ [RequireComponent(typeof(Rigidbody2D))]
73
+ public class Spaceship : MonoBehaviour {
74
+ public float speed;
75
+ public float shotDelay;
76
+ public GameObject bullet;
77
+ public void Shot (Transform origin)
78
+ {
79
+ Instantiate(bullet, origin.position, origin.rotation);
80
+ }
81
+
82
+ public void Move(Vector2 direction)
83
+ {
84
+ GetComponent<Rigidbody2D>().velocity = direction * speed;
85
+ }
86
+ }
87
+
88
+ ```
89
+
90
+ ### 試したこと
91
+ このエラーについてネットで検索して、弾が消された後にスクリプトで弾が存在することを要求してしまっているのでエラーが起きていのではないかと思いました。しかし、それをどうやったら解消できるのかわかりません。
92
+
93
+
94
+ ### 前提・実現したいこと
95
+ "MissingReferenceException"をなくして、プレイヤーから正常に弾が放出されるようにしたいです。
96
+
97
+
98
+ ### 補足情報(言語/FW/ツール等のバージョンなど)
99
+ サイトのチュートリアルではプレイヤーや敵が被弾した際に爆発するスクリプト・画像を導入しているのですが、私は画像を自分で描いていて、爆発の絵を描いていないためスクリプトの爆発に関した部分を省略しています。その省略で必要な命令が欠けてエラーが起きてしまったのかもしれないと思いました(サイトのお手本見比べましたが自分ではわからなかったです)。
100
+ 使用言語はC♯、Unityはver.5.5.1です。
99
101
  よろしくお願いします。