質問編集履歴
4
プログラムの変更
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
|
1
|
+
複数の敵キャラの中で自機から弾が出て当たると敵キャラが消えるソースを作りたい。
|
body
CHANGED
@@ -1,242 +1,8 @@
|
|
1
1
|
Unity 初心者です。
|
2
|
+
複数の敵キャラの中で自機から弾が出て当たると敵キャラが消えるソースを作りたい。
|
2
|
-
Unity にて、prefabにBox Collider 2D設定を
|
3
|
+
Unity にて、prefabにBox Collider 2D設定し下記ソースを、敵キャラクターPrefabに設定したのですが、
|
3
4
|
|
4
|
-
```ここに言語を入力
|
5
|
-
コード
|
6
|
-
using System.Collections;
|
7
|
-
using System.Collections.Generic;
|
8
|
-
using UnityEngine;
|
9
|
-
using System;
|
10
|
-
|
11
|
-
|
12
|
-
static class GlobalVariables
|
13
|
-
{
|
14
|
-
static public bool Shoot_Flag = false;
|
15
|
-
}
|
16
|
-
|
17
|
-
[System.Serializable]
|
18
|
-
public struct Human_Status
|
19
|
-
{
|
20
|
-
public GameObject Human; //敵キャラのx,y座標
|
21
|
-
public int Dir; //敵キャラの横移動、縦移動
|
22
|
-
public int Atack_Counter; //敵キャラの縦移動を決めるカウンター
|
23
|
-
public int T_Counter; //敵キャラのタイミングカウンター
|
24
|
-
public float Ho_x; //敵キャラの出現位置
|
25
|
-
public float Ho_y; //敵キャラの出現位置
|
26
|
-
|
27
|
-
}
|
28
|
-
|
29
|
-
public class Deseart_Hyouji : MonoBehaviour
|
30
|
-
{
|
31
|
-
private GameObject[,] Desert_obj;
|
32
|
-
}
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
public class Desert_load5 : MonoBehaviour
|
37
|
-
{
|
38
|
-
public int HUMAN_num = 7;
|
39
|
-
public List<Human_Status> Chara = new List<Human_Status>();
|
40
|
-
public Human_Status human_status = new Human_Status();
|
41
|
-
private GameObject Trump_obj;
|
42
|
-
private float tx = 0.0f;
|
43
|
-
private float ty = -2.75f;
|
44
|
-
private float tz = 0.00f;
|
45
|
-
|
46
|
-
private Vector3 pos;
|
47
|
-
private Vector3 WorldPointPos;
|
48
|
-
private float point_x = 0.0f;
|
49
|
-
private float point_y = 0.0f;
|
50
|
-
private float rad;
|
51
|
-
private float dx;
|
52
|
-
private float dy;
|
53
|
-
private float r_dx;
|
54
|
-
private float r_dy;
|
55
|
-
private float S_x;
|
56
|
-
private float S_y;
|
57
|
-
|
58
|
-
public GameObject Shot_obj;
|
59
|
-
public GameObject Taget_obj;
|
60
|
-
private int rote_z = 0;
|
61
|
-
|
62
|
-
void Start()
|
63
|
-
{
|
64
|
-
Deseart_Hyouji hyouji = new Deseart_Hyouji();
|
65
|
-
hyouji.Hyouji();
|
66
|
-
GameObject Mexico_Man_obj = new GameObject();
|
67
|
-
human_status.Human = Mexico_Man_obj;
|
68
|
-
human_status.Human.name = "Mexico_Man";
|
69
|
-
for (int k = 0; k < HUMAN_num; k++)
|
70
|
-
{
|
71
|
-
Vector2 tmp2 = GameObject.Find("Mexico_Man").transform.position;
|
72
|
-
GameObject.Find("Mexico_Man").transform.position = new Vector2(UnityEngine.Random.Range(-1.0f, 2.3f), UnityEngine.Random.Range(1.8f, 4.6f));
|
73
|
-
tmp2.x = UnityEngine.Random.Range(-1.0f, 2.3f);
|
74
|
-
tmp2.y = UnityEngine.Random.Range(1.8f, 4.6f);
|
75
|
-
Mexico_Man_obj = Instantiate(Resources.Load("Mexicoman_pref"), new Vector2(tmp2.x, tmp2.y), Quaternion.identity) as GameObject;
|
76
|
-
human_status.Human = Mexico_Man_obj;
|
77
|
-
human_status.Dir = UnityEngine.Random.Range(1, 3);
|
78
|
-
human_status.T_Counter = 0;
|
79
|
-
human_status.Ho_x = 0.00f;
|
80
|
-
human_status.Ho_y = 0.00f;
|
81
|
-
|
82
|
-
Chara.Add(human_status);
|
83
|
-
}
|
84
|
-
Trump_obj = Instantiate(Resources.Load("trump_pref"), new Vector3(tx, ty, tz), Quaternion.identity) as GameObject;
|
85
|
-
Trump_obj.name = "Trump";
|
86
|
-
}
|
87
|
-
|
88
|
-
void Update()
|
89
|
-
{
|
90
|
-
for (int i = 0; i < HUMAN_num; i++)
|
91
|
-
{
|
92
|
-
Human_Status human_status = Chara[i];
|
93
|
-
|
94
|
-
Vector3 pos = human_status.Human.transform.position;
|
95
|
-
human_status.T_Counter = human_status.T_Counter + 1;
|
96
|
-
|
97
|
-
if (human_status.T_Counter > 5)
|
98
|
-
{
|
99
|
-
human_status.T_Counter = 0;
|
100
|
-
human_status.Atack_Counter = UnityEngine.Random.Range(1, 9);
|
101
|
-
|
102
|
-
if (human_status.Atack_Counter == 6)
|
103
|
-
{
|
104
|
-
human_status.Dir = 3;
|
105
|
-
}
|
106
|
-
|
107
|
-
}
|
108
|
-
if (human_status.Dir == 1)
|
109
|
-
{
|
110
|
-
|
111
|
-
pos.x = pos.x - 0.05f;
|
112
|
-
human_status.Human.transform.position = pos;
|
113
|
-
if (human_status.Human.transform.position.x < -1.62f)
|
114
|
-
{
|
115
|
-
human_status.Dir = 2;
|
116
|
-
}
|
117
|
-
else if ((human_status.Atack_Counter == 7) || (human_status.Atack_Counter == 8))
|
118
|
-
{
|
119
|
-
human_status.Dir = 2;
|
120
|
-
human_status.Atack_Counter = 0;
|
121
|
-
}
|
122
|
-
}
|
123
|
-
else if (human_status.Dir == 2)
|
124
|
-
{
|
125
|
-
pos.x = pos.x + 0.05f;
|
126
|
-
human_status.Human.transform.position = pos;
|
127
|
-
if (human_status.Human.transform.position.x > 2.3f)
|
128
|
-
{
|
129
|
-
human_status.Dir = 1;
|
130
|
-
}
|
131
|
-
else if ((human_status.Atack_Counter == 1) || (human_status.Atack_Counter == 2))
|
132
|
-
{
|
133
|
-
human_status.Dir = 1;
|
134
|
-
human_status.Atack_Counter = 0;
|
135
|
-
}
|
136
|
-
}
|
137
|
-
else if (human_status.Dir == 3)
|
138
|
-
{
|
139
|
-
if (human_status.Human.transform.position.y < -5.0f)
|
140
|
-
{
|
141
|
-
human_status.Ho_x = UnityEngine.Random.Range(-1.0f, 2.3f);//最小-1.2f(-1.0f)最大(2.5f)2.3f
|
142
|
-
human_status.Human.transform.position = new Vector3(human_status.Ho_x, 5.0f, 0.00f);
|
143
|
-
}
|
144
|
-
else if (human_status.Human.transform.position.y > 4.6f)
|
145
|
-
{
|
146
|
-
human_status.Human.transform.position = new Vector3(human_status.Human.transform.position.x, human_status.Human.transform.position.y + -0.05f, 0.00f);
|
147
|
-
}
|
148
|
-
|
149
|
-
else
|
150
|
-
{
|
151
|
-
human_status.Human.transform.position = new Vector3(human_status.Human.transform.position.x, human_status.Human.transform.position.y + -0.05f, 0.00f);
|
152
|
-
human_status.Ho_y = UnityEngine.Random.Range(1.8f, 4.6f);//最小1.8f最大4.6f
|
153
|
-
if (human_status.Ho_y <= 4.6f)
|
154
|
-
{
|
155
|
-
if ((human_status.Ho_y >= 1.8f) | (human_status.Ho_y <= 4.6f))
|
156
|
-
{
|
157
|
-
if (human_status.Human.transform.position.y > human_status.Ho_y)
|
158
|
-
{
|
159
|
-
human_status.Dir = UnityEngine.Random.Range(1, 4);
|
160
|
-
}
|
161
|
-
}
|
162
|
-
|
163
|
-
}
|
164
|
-
}
|
165
|
-
|
166
|
-
}
|
167
|
-
Chara[i] = human_status;
|
168
|
-
}
|
169
|
-
if (Input.GetMouseButtonDown(0) && GlobalVariables.Shoot_Flag == false)
|
170
|
-
{
|
171
|
-
|
172
|
-
//マウス位置座標をVector3で取得
|
173
|
-
pos = Input.mousePosition;
|
174
|
-
// マウス位置座標をスクリーン座標からワールド座標に変換する
|
175
|
-
WorldPointPos = Camera.main.ScreenToWorldPoint(pos);
|
176
|
-
point_x = WorldPointPos.x;
|
177
|
-
point_y = WorldPointPos.y;
|
178
|
-
|
179
|
-
if (point_x < -1.39f)
|
180
|
-
{
|
181
|
-
point_x = -1.35f;
|
182
|
-
|
183
|
-
}
|
184
|
-
if (point_y < -4.25f)
|
185
|
-
{
|
186
|
-
point_y = -4.25f;
|
187
|
-
|
188
|
-
}
|
189
|
-
|
190
|
-
//trumpの座標を取る。
|
191
|
-
Vector3 tmp3 = GameObject.Find("Trump").transform.position;
|
192
|
-
GameObject.Find("Trump").transform.position = new Vector3(tmp3.x, tmp3.y, tmp3.z);
|
193
|
-
|
194
|
-
dx = tmp3.x;
|
195
|
-
dy = tmp3.y;
|
196
|
-
r_dx = point_x - dx;
|
197
|
-
r_dy = point_y - dy;
|
198
|
-
rad = Mathf.Atan2(r_dy, r_dx);
|
199
|
-
|
200
|
-
Shot_obj = Instantiate(Resources.Load("Frying_pan_pref"), new Vector3(dx, dy, 0.0f), Quaternion.identity) as GameObject;
|
201
|
-
Taget_obj = Instantiate(Resources.Load("taget3_pref"), new Vector3(point_x, point_y, 0.0f), Quaternion.identity) as GameObject;
|
202
|
-
Shot_obj.transform.Rotate(new Vector3(0, 0, rad * 45.0f));
|
203
|
-
GlobalVariables.Shoot_Flag = true;
|
204
|
-
}
|
205
|
-
else if(GlobalVariables.Shoot_Flag == true)
|
206
|
-
{
|
207
|
-
Vector3 Position = Shot_obj.transform.position;
|
208
|
-
float xx = Mathf.Cos(rad);
|
209
|
-
float yy = Mathf.Sin(rad);
|
210
|
-
xx *= -0.1f;//idatenn
|
211
|
-
yy *= -0.1f;//idatenn
|
212
|
-
Position.x -= xx; //* Time.deltaTime ;
|
213
|
-
Position.y -= yy; //* Time.deltaTime;
|
214
|
-
Debug.Log("xx=" + xx);
|
215
|
-
Debug.Log("yy=" + yy);
|
216
|
-
|
217
|
-
S_x = Position.x;
|
218
|
-
S_y = Position.y;
|
219
|
-
Shot_obj.transform.position = new Vector3(S_x, S_y, 0.0f);
|
220
|
-
|
221
|
-
// Shot_obj.transform.Rotate(new Vector3(0, 0, rote_z));
|
222
|
-
// rote_z = rote_z + 30;
|
223
|
-
|
224
|
-
if (rote_z == 360)
|
225
|
-
{
|
226
|
-
rote_z = 0;
|
227
|
-
}
|
228
|
-
}
|
229
|
-
}
|
230
|
-
}
|
231
|
-
|
232
5
|
```
|
233
|
-
|
234
|
-
"Mexicoman_pref"にBox Collider 2D設定をすると、動きがおかしくなり困ってます。
|
235
|
-
|
236
|
-
Box Collider 2Dと下記スクリプトを追加すると動きは、おかしくならないようになったのですが、
|
237
|
-
コンソールに代わりにnullで止ま表示され止まり動きが止まるキャラクタが出ます。
|
238
|
-
解決方法をアドバイス下さい。
|
239
|
-
```ここに言語を入力
|
240
6
|
using System.Collections;
|
241
7
|
using System.Collections.Generic;
|
242
8
|
using UnityEngine;
|
@@ -245,32 +11,55 @@
|
|
245
11
|
|
246
12
|
public GameObject M_obj;
|
247
13
|
|
248
|
-
|
249
|
-
|
250
|
-
|
251
14
|
// Use this for initialization
|
252
15
|
void Start () {
|
253
16
|
|
254
|
-
|
17
|
+
}
|
255
18
|
|
256
|
-
|
19
|
+
// Update is called once per frame
|
257
|
-
|
20
|
+
void Update () {
|
258
|
-
|
21
|
+
|
259
|
-
|
22
|
+
}
|
260
23
|
void OnTriggerEnter2D(Collider2D c)
|
261
24
|
{
|
262
25
|
if (c.gameObject.tag == "Hit")
|
263
26
|
{
|
264
|
-
|
27
|
+
GameObject.Destroy(M_obj);
|
265
|
-
|
28
|
+
M_obj = null;
|
266
29
|
}
|
267
|
-
|
30
|
+
if (M_obj == null)
|
31
|
+
{
|
32
|
+
Debug.Log("hoge hoge");
|
33
|
+
}
|
34
|
+
if (M_obj != null)
|
35
|
+
{
|
36
|
+
Debug.Log("homo homo");
|
37
|
+
}
|
268
|
-
Debug.Log("
|
38
|
+
Debug.Log("hit3 Object COLLISION");
|
269
39
|
Debug.Log("M_obj " + M_obj);
|
270
40
|
return;
|
271
41
|
}
|
272
|
-
|
273
42
|
}
|
274
43
|
|
275
44
|
コード
|
276
|
-
```
|
45
|
+
```
|
46
|
+
エラーコードから、
|
47
|
+
MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
|
48
|
+
Your script should either check if it is null or you should not destroy the object.
|
49
|
+
Desert_load5.Update () (at Assets/Desert_load5.cs:151)
|
50
|
+
該当行で、
|
51
|
+
|
52
|
+
``` void Update()
|
53
|
+
{
|
54
|
+
for (int i = 0; i < HUMAN_num; i++)
|
55
|
+
{
|
56
|
+
Human_Status human_status = Chara[i];
|
57
|
+
|
58
|
+
Vector3 pos = human_status.Human.transform.position;//151行目
|
59
|
+
human_status.T_Counter = human_status.T_Counter + 1;
|
60
|
+
|
61
|
+
|
62
|
+
コード
|
63
|
+
```
|
64
|
+
プレファブをnullにした後、どのように振るまっていいのか解りません。
|
65
|
+
アドバイスの程、よろしくお願いいたします。
|
3
Box Collider 2Dと下記スクリプトを追加しまた。
title
CHANGED
@@ -1,1 +1,1 @@
|
|
1
|
-
prefabにBox Collider 2D
|
1
|
+
prefabにBox Collider 2Dのnullで困っています。
|
body
CHANGED
@@ -11,8 +11,6 @@
|
|
11
11
|
|
12
12
|
static class GlobalVariables
|
13
13
|
{
|
14
|
-
|
15
|
-
|
16
14
|
static public bool Shoot_Flag = false;
|
17
15
|
}
|
18
16
|
|
@@ -28,42 +26,9 @@
|
|
28
26
|
|
29
27
|
}
|
30
28
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
29
|
public class Deseart_Hyouji : MonoBehaviour
|
36
30
|
{
|
37
31
|
private GameObject[,] Desert_obj;
|
38
|
-
|
39
|
-
public void Hyouji()
|
40
|
-
{
|
41
|
-
Desert_obj = new GameObject[16, 9];
|
42
|
-
|
43
|
-
for (int i = 0; i < 16; i++)
|
44
|
-
{
|
45
|
-
|
46
|
-
for (int j = 0; j < 9; j++)
|
47
|
-
{
|
48
|
-
if ((i == 5) /*& (j >= 2)*/)
|
49
|
-
{
|
50
|
-
Desert_obj[i, j] = Instantiate(Resources.Load("Border_pref"), new Vector2(-2.9f + (0.645f * j), 5.0f - (0.645f * i)), Quaternion.identity) as GameObject;
|
51
|
-
}
|
52
|
-
// else if (j <= 1)
|
53
|
-
// {
|
54
|
-
// Desert_obj[i, j] = Instantiate(Resources.Load("b_choice"), new Vector2(-2.9f + (0.64f * j), 5.0f - (0.64f * i)), Quaternion.identity) as GameObject;
|
55
|
-
// }
|
56
|
-
else if (i >= 13)
|
57
|
-
{
|
58
|
-
Desert_obj[i, j] = Instantiate(Resources.Load("b_choice"), new Vector2(-2.9f + (0.64f * j), 5.0f - (0.64f * i)), Quaternion.identity) as GameObject;
|
59
|
-
}
|
60
|
-
else
|
61
|
-
{
|
62
|
-
Desert_obj[i, j] = Instantiate(Resources.Load("Desert_pref"), new Vector2(-2.9f + (0.645f * j), 5.0f - (0.645f * i)), Quaternion.identity) as GameObject;
|
63
|
-
}
|
64
|
-
}
|
65
|
-
}
|
66
|
-
}
|
67
32
|
}
|
68
33
|
|
69
34
|
|
@@ -266,4 +231,46 @@
|
|
266
231
|
|
267
232
|
```
|
268
233
|
|
269
|
-
"Mexicoman_pref"にBox Collider 2D設定をすると、動きがおかしくなり困ってます。
|
234
|
+
"Mexicoman_pref"にBox Collider 2D設定をすると、動きがおかしくなり困ってます。
|
235
|
+
|
236
|
+
Box Collider 2Dと下記スクリプトを追加すると動きは、おかしくならないようになったのですが、
|
237
|
+
コンソールに代わりにnullで止ま表示され止まり動きが止まるキャラクタが出ます。
|
238
|
+
解決方法をアドバイス下さい。
|
239
|
+
```ここに言語を入力
|
240
|
+
using System.Collections;
|
241
|
+
using System.Collections.Generic;
|
242
|
+
using UnityEngine;
|
243
|
+
|
244
|
+
public class Trigger3 : MonoBehaviour {
|
245
|
+
|
246
|
+
public GameObject M_obj;
|
247
|
+
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
// Use this for initialization
|
252
|
+
void Start () {
|
253
|
+
|
254
|
+
}
|
255
|
+
|
256
|
+
// Update is called once per frame
|
257
|
+
void Update () {
|
258
|
+
|
259
|
+
}
|
260
|
+
void OnTriggerEnter2D(Collider2D c)
|
261
|
+
{
|
262
|
+
if (c.gameObject.tag == "Hit")
|
263
|
+
{
|
264
|
+
Destroy(M_obj);
|
265
|
+
|
266
|
+
}
|
267
|
+
|
268
|
+
Debug.Log("hit2 Object COLLISION");
|
269
|
+
Debug.Log("M_obj " + M_obj);
|
270
|
+
return;
|
271
|
+
}
|
272
|
+
|
273
|
+
}
|
274
|
+
|
275
|
+
コード
|
276
|
+
```
|
2
prefabに下記スクリプトを追加する事で動きがおかしくなる現象は、なくなったのですが、代わりにnullが返って来ておかしくなる
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|
1
prefabにスクリプトを追加すると、nullが返ってきます。
title
CHANGED
File without changes
|
body
CHANGED
File without changes
|