質問編集履歴
1
書式変更
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
|
-
|
11
|
-
using System.Collections
|
12
|
-
using
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
public
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
}
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
using System.Collections
|
29
|
-
using
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
float
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
}
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
using System.Collections
|
68
|
-
using
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
public float
|
75
|
-
public
|
76
|
-
public
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
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
|
よろしくお願いします。
|