質問編集履歴

3

まだまだ初心者でした

2018/07/27 14:30

投稿

UDON3
UDON3

スコア80

test CHANGED
File without changes
test CHANGED
File without changes

2

加筆

2018/07/27 14:30

投稿

UDON3
UDON3

スコア80

test CHANGED
File without changes
test CHANGED
@@ -1,6 +1,6 @@
1
1
  ### 現状
2
2
 
3
- 外部ファイルで文字列を保存したいのですが、データパス指定でエラーが出ました。
3
+ 外部ファイルで文字列を保存したいのですが、
4
4
 
5
5
  ```C#
6
6
 
@@ -8,7 +8,535 @@
8
8
 
9
9
  ```
10
10
 
11
- このパス指定のコードを追加した際にエラーが出ました。
11
+ このパスを変数に代入するコードを追加した際にエラーが出ました。
12
+
13
+ このコードを追加したスクリプト全体はこちらです。
14
+
15
+ ```using System.Collections;
16
+
17
+ using System.Collections.Generic;
18
+
19
+ using UnityEngine;
20
+
21
+ using System;
22
+
23
+ using System.Text;
24
+
25
+ using UnityEngine.UI;
26
+
27
+ using TMPro;
28
+
29
+ using System.IO;
30
+
31
+
32
+
33
+ public class Controller : MonoBehaviour {
34
+
35
+ public static bool play;
36
+
37
+ static public int SphereID;
38
+
39
+ static public bool select;
40
+
41
+ static public bool playstart;
42
+
43
+ static public bool HotbarButtonselect;
44
+
45
+ static public Vector2 Pos;
46
+
47
+ static public bool locking;
48
+
49
+ static public int[] hotID = new int[8];
50
+
51
+ static public int writehotID;
52
+
53
+ static public int stage = 1;
54
+
55
+ static public bool Clear;
56
+
57
+ static public bool stop;
58
+
59
+ static public bool stopnow = false;
60
+
61
+ static public bool resumenow =false;
62
+
63
+ [SerializeField]
64
+
65
+ static public float sensitivity = 0.2f;
66
+
67
+ static public bool GameOver;
68
+
69
+ public GameObject FAILED;
70
+
71
+ public GameObject CLEAR;
72
+
73
+ static public bool showCursor;
74
+
75
+ static public bool hideCursor;
76
+
77
+ public Text Pkey_text;
78
+
79
+ static public bool actionnow;
80
+
81
+ static public float nowtimeScale;
82
+
83
+ public TextMeshProUGUI Text_Source;
84
+
85
+ static public string WarningText;
86
+
87
+ static public string TargetScene;
88
+
89
+ public GameObject MenuObj;
90
+
91
+ static public bool OpenTheMenu = false;
92
+
93
+ // Use this for initialization
94
+
95
+ static public bool OpenTheTestData;
96
+
97
+ static public string GameOverLog;
98
+
99
+ public Text FAILEDInfoText;
100
+
101
+ //DataBase_Start-------------------------------------------------------------------------------------------------------------------------------------
102
+
103
+ public GameObject ConfilmObj;
104
+
105
+ static public List<int> IDData = new List<int>();
106
+
107
+ static public List<Vector3> PositionData = new List<Vector3>();
108
+
109
+ static public List<Quaternion> RotationData = new List<Quaternion>();
110
+
111
+ static public List<GameObject> Targets = new List<GameObject>();
112
+
113
+ static public List<bool> Destroyed = new List<bool>();
114
+
115
+ static public int TotalBlocks;
116
+
117
+ static public bool editOK;
118
+
119
+ public string[] Display_Data = { "_FALIED", "F_AILED", "FA_IELD", "FAI_LED", "FAIL_ED", "FAILE_D", "FAILED_" };
120
+
121
+ private string DataPath = Application.dataPath + "/original_data.txt";
122
+
123
+ string SaveData;
124
+
125
+ //DataBase_End---------------------------------------------------------------------------------------------------------------------------------------
126
+
127
+ // Use this for initialization
128
+
129
+ void Start () {
130
+
131
+ showCursor = false;
132
+
133
+ hideCursor = true;
134
+
135
+ actionnow = false;
136
+
137
+ nowtimeScale = 1;
138
+
139
+ GameOver = false;
140
+
141
+ playstart = false;
142
+
143
+ stop = false;
144
+
145
+ play = false;
146
+
147
+ TotalBlocks = 0;
148
+
149
+ SphereID = 0;
150
+
151
+ select = false;
152
+
153
+ HotbarButtonselect = false;
154
+
155
+ locking = false;
156
+
157
+ Clear = false;
158
+
159
+ StartCoroutine(SwitchText());
160
+
161
+ ImportantSceneButton.ConfilmObj = ConfilmObj;
162
+
163
+ OpenTheTestData = false;
164
+
165
+ editOK = true;
166
+
167
+ }
168
+
169
+
170
+
171
+ // Update is called once per frame
172
+
173
+ void Update()
174
+
175
+ {
176
+
177
+ //Tehon----------------------------------------------------------
178
+
179
+ if (Input.GetKeyDown("t"))
180
+
181
+ {
182
+
183
+ if (OpenTheTestData == false)
184
+
185
+ {
186
+
187
+ editOK = false;
188
+
189
+ OpenTheTestData = true;
190
+
191
+ foreach (GameObject Target in Targets)
192
+
193
+ {
194
+
195
+ Destroy(Target);
196
+
197
+ }
198
+
199
+ }
200
+
201
+ else
202
+
203
+ {
204
+
205
+ editOK = true;
206
+
207
+ OpenTheTestData = false;
208
+
209
+ for(int i = 0;i <= 100; i++)
210
+
211
+ {
212
+
213
+ if (Destroyed[i] == false)
214
+
215
+ {
216
+
217
+ GameObject Prefab = Instantiate(RayScript.DataObj[IDData[i]], PositionData[i], RotationData[i]) as GameObject;
218
+
219
+ Prefab.name = Convert.ToString(i);
220
+
221
+ Targets.Insert(i, Prefab);
222
+
223
+ }
224
+
225
+ else
226
+
227
+ {
228
+
229
+ Targets.Insert(i, null);
230
+
231
+ }
232
+
233
+ }
234
+
235
+ }
236
+
237
+ }
238
+
239
+ //Save-----------------------------------------------------
240
+
241
+ if (Input.GetKeyDown("s"))
242
+
243
+ {
244
+
245
+ JSONvar vardatas = new JSONvar();
246
+
247
+ vardatas.IDData = IDData;
248
+
249
+ vardatas.PositionData = PositionData;
250
+
251
+ vardatas.RotationData = RotationData;
252
+
253
+ vardatas.Targets = Targets;
254
+
255
+ vardatas.Destroyed = Destroyed;
256
+
257
+ SaveData = JsonUtility.ToJson(vardatas);
258
+
259
+ Debug.Log(SaveData);
260
+
261
+ File.WriteAllText(DataPath, SaveData, System.Text.Encoding.UTF8);
262
+
263
+ }
264
+
265
+ if (Input.GetKeyDown("o"))
266
+
267
+ {
268
+
269
+ JSONvar roaddata = JsonUtility.FromJson<JSONvar>(SaveData);
270
+
271
+ IDData = roaddata.IDData;
272
+
273
+ PositionData = roaddata.PositionData;
274
+
275
+ RotationData = roaddata.RotationData;
276
+
277
+ Targets = roaddata.Targets;
278
+
279
+ Destroyed = roaddata.Destroyed;
280
+
281
+ }
282
+
283
+ //play----------------------------------------------------
284
+
285
+ if (Input.GetKeyDown("p") && actionnow == false)
286
+
287
+ {
288
+
289
+ if (play == false)
290
+
291
+ {
292
+
293
+ play = true;
294
+
295
+ stop = false;
296
+
297
+ resumenow = true;
298
+
299
+ nowtimeScale = 1;
300
+
301
+ playstart = true;
302
+
303
+ GameObject[] Spawners = GameObject.FindGameObjectsWithTag("Spawner");
304
+
305
+ foreach (GameObject Spawner in Spawners)
306
+
307
+ {
308
+
309
+ Spawner.GetComponent<Spawn>().Spawner();
310
+
311
+ }
312
+
313
+ }
314
+
315
+ else if (play == true)
316
+
317
+ {
318
+
319
+ ResetGame();
320
+
321
+ }
322
+
323
+ }
324
+
325
+ //menu-----------------------------------------------------------------------
326
+
327
+ if (Input.GetKeyDown("m"))
328
+
329
+ {
330
+
331
+ if (MenuObj.activeSelf)
332
+
333
+ {
334
+
335
+ MenuObj.SetActive(false);
336
+
337
+ hideCursor = true;
338
+
339
+ actionnow = false;
340
+
341
+ }
342
+
343
+ else
344
+
345
+ {
346
+
347
+ MenuObj.SetActive(true);
348
+
349
+ showCursor = true;
350
+
351
+ actionnow = true;
352
+
353
+ }
354
+
355
+ }
356
+
357
+ if (play)
358
+
359
+ {
360
+
361
+ Pkey_text.text = "中断";
362
+
363
+ }
364
+
365
+ else
366
+
367
+ {
368
+
369
+ Pkey_text.text = "スタート";
370
+
371
+ }
372
+
373
+ if (stopnow && play == true)
374
+
375
+ {
376
+
377
+ stopnow = false;
378
+
379
+ Time.timeScale = 0;
380
+
381
+ }
382
+
383
+ else if (resumenow)
384
+
385
+ {
386
+
387
+ resumenow = false;
388
+
389
+ Time.timeScale = nowtimeScale;
390
+
391
+ }
392
+
393
+ if (showCursor == true)
394
+
395
+ {
396
+
397
+ Cursor.visible = true;
398
+
399
+ Cursor.lockState = CursorLockMode.None;
400
+
401
+ locking = true;
402
+
403
+ showCursor = false;
404
+
405
+ }
406
+
407
+ else if (hideCursor == true)
408
+
409
+ {
410
+
411
+ Cursor.visible = false;
412
+
413
+ Cursor.lockState = CursorLockMode.Locked;
414
+
415
+ locking = false;
416
+
417
+ hideCursor = false;
418
+
419
+ }
420
+
421
+ if(GameOver == true)
422
+
423
+ {
424
+
425
+ FAILED.SetActive(true);
426
+
427
+ Controller.GameOver = true;
428
+
429
+ Controller.showCursor = true;
430
+
431
+ Controller.actionnow = true;
432
+
433
+ FAILEDInfoText.text = GameOverLog;
434
+
435
+ }
436
+
437
+ }
438
+
439
+ public void RetryButton()
440
+
441
+ {
442
+
443
+ FAILED.SetActive(false);
444
+
445
+ CLEAR.SetActive(false);
446
+
447
+ ResetGame();
448
+
449
+ hideCursor = true;
450
+
451
+ GameOver = false;
452
+
453
+ actionnow = false;
454
+
455
+ Clear = false;
456
+
457
+ TotalBlocks = 0;
458
+
459
+ SphereID = 0;
460
+
461
+ }
462
+
463
+
464
+
465
+ void ResetGame()
466
+
467
+ {
468
+
469
+ play = false;
470
+
471
+ GameObject[] destroys = GameObject.FindGameObjectsWithTag("Player");
472
+
473
+ foreach (GameObject destroy in destroys)
474
+
475
+ {
476
+
477
+ Destroy(destroy);
478
+
479
+ }
480
+
481
+ GameObject[] Dummys = GameObject.FindGameObjectsWithTag("Dummy");
482
+
483
+ foreach (GameObject Dummy in Dummys)
484
+
485
+ {
486
+
487
+ Destroy(Dummy);
488
+
489
+ }
490
+
491
+ stop = false;
492
+
493
+ resumenow = true;
494
+
495
+ nowtimeScale = 1;
496
+
497
+ GameObject[] Spawners = GameObject.FindGameObjectsWithTag("Spawner");
498
+
499
+ foreach (GameObject Spawner in Spawners)
500
+
501
+ {
502
+
503
+ Spawner.GetComponent<Spawn>().Reset();
504
+
505
+ }
506
+
507
+ }
508
+
509
+
510
+
511
+ IEnumerator SwitchText()
512
+
513
+ {
514
+
515
+ while (true)
516
+
517
+ {
518
+
519
+ for (int i = 0; i <= Display_Data.Length - 1; i++)
520
+
521
+ {
522
+
523
+ Text_Source.SetText(Display_Data[i]);
524
+
525
+ yield return new WaitForSeconds(1f);
526
+
527
+ }
528
+
529
+ }
530
+
531
+ }
532
+
533
+ }
534
+
535
+
536
+
537
+ コード
538
+
539
+ ```
12
540
 
13
541
  エラーは
14
542
 

1

エラーコード加筆

2018/07/27 14:20

投稿

UDON3
UDON3

スコア80

test CHANGED
File without changes
test CHANGED
@@ -10,6 +10,32 @@
10
10
 
11
11
  このパス指定のコードを追加した際にエラーが出ました。
12
12
 
13
+ エラーは
14
+
15
+ ```ここに言語を入力
16
+
17
+ UnityException: get_dataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'Controller' on game object 'GameCon (1)'.
18
+
19
+ See "Script Serialization" page in the Unity Manual for further details.
20
+
21
+ Controller..ctor () (at Assets/Scouse/Controller.cs:54)
22
+
23
+ ```
24
+
25
+
26
+
27
+ ```ここに言語を入力
28
+
29
+ UnityException: get_dataPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'Controller' on game object 'GameCon (1)'.
30
+
31
+ See "Script Serialization" page in the Unity Manual for further details.
32
+
33
+ Controller..ctor () (at Assets/Scouse/Controller.cs:54)
34
+
35
+ ```
36
+
37
+ が何度も出てきます
38
+
13
39
  書き方に問題があるはずなのですが、何がいけないんでしょうか。
14
40
 
15
41
  分かる方が見たらすぐわかると思いますが、数時間調べてもわかりませんでした、誰か回答お願いします。