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

質問編集履歴

3

まだまだ初心者でした

2018/07/27 14:30

投稿

UDON3
UDON3

スコア80

title CHANGED
File without changes
body CHANGED
File without changes

2

加筆

2018/07/27 14:30

投稿

UDON3
UDON3

スコア80

title CHANGED
File without changes
body CHANGED
@@ -1,9 +1,273 @@
1
1
  ### 現状
2
- 外部ファイルで文字列を保存したいのですが、データパス指定でエラーが出ました。
2
+ 外部ファイルで文字列を保存したいのですが、
3
3
  ```C#
4
4
  private string DetaPath = Application.dataPath + "/original_data.txt";
5
5
  ```
6
- このパス指定のコードを追加した際にエラーが出ました。
6
+ このパスを変数に代入するコードを追加した際にエラーが出ました。
7
+ このコードを追加したスクリプト全体はこちらです。
8
+ ```using System.Collections;
9
+ using System.Collections.Generic;
10
+ using UnityEngine;
11
+ using System;
12
+ using System.Text;
13
+ using UnityEngine.UI;
14
+ using TMPro;
15
+ using System.IO;
16
+
17
+ public class Controller : MonoBehaviour {
18
+ public static bool play;
19
+ static public int SphereID;
20
+ static public bool select;
21
+ static public bool playstart;
22
+ static public bool HotbarButtonselect;
23
+ static public Vector2 Pos;
24
+ static public bool locking;
25
+ static public int[] hotID = new int[8];
26
+ static public int writehotID;
27
+ static public int stage = 1;
28
+ static public bool Clear;
29
+ static public bool stop;
30
+ static public bool stopnow = false;
31
+ static public bool resumenow =false;
32
+ [SerializeField]
33
+ static public float sensitivity = 0.2f;
34
+ static public bool GameOver;
35
+ public GameObject FAILED;
36
+ public GameObject CLEAR;
37
+ static public bool showCursor;
38
+ static public bool hideCursor;
39
+ public Text Pkey_text;
40
+ static public bool actionnow;
41
+ static public float nowtimeScale;
42
+ public TextMeshProUGUI Text_Source;
43
+ static public string WarningText;
44
+ static public string TargetScene;
45
+ public GameObject MenuObj;
46
+ static public bool OpenTheMenu = false;
47
+ // Use this for initialization
48
+ static public bool OpenTheTestData;
49
+ static public string GameOverLog;
50
+ public Text FAILEDInfoText;
51
+ //DataBase_Start-------------------------------------------------------------------------------------------------------------------------------------
52
+ public GameObject ConfilmObj;
53
+ static public List<int> IDData = new List<int>();
54
+ static public List<Vector3> PositionData = new List<Vector3>();
55
+ static public List<Quaternion> RotationData = new List<Quaternion>();
56
+ static public List<GameObject> Targets = new List<GameObject>();
57
+ static public List<bool> Destroyed = new List<bool>();
58
+ static public int TotalBlocks;
59
+ static public bool editOK;
60
+ public string[] Display_Data = { "_FALIED", "F_AILED", "FA_IELD", "FAI_LED", "FAIL_ED", "FAILE_D", "FAILED_" };
61
+ private string DataPath = Application.dataPath + "/original_data.txt";
62
+ string SaveData;
63
+ //DataBase_End---------------------------------------------------------------------------------------------------------------------------------------
64
+ // Use this for initialization
65
+ void Start () {
66
+ showCursor = false;
67
+ hideCursor = true;
68
+ actionnow = false;
69
+ nowtimeScale = 1;
70
+ GameOver = false;
71
+ playstart = false;
72
+ stop = false;
73
+ play = false;
74
+ TotalBlocks = 0;
75
+ SphereID = 0;
76
+ select = false;
77
+ HotbarButtonselect = false;
78
+ locking = false;
79
+ Clear = false;
80
+ StartCoroutine(SwitchText());
81
+ ImportantSceneButton.ConfilmObj = ConfilmObj;
82
+ OpenTheTestData = false;
83
+ editOK = true;
84
+ }
85
+
86
+ // Update is called once per frame
87
+ void Update()
88
+ {
89
+ //Tehon----------------------------------------------------------
90
+ if (Input.GetKeyDown("t"))
91
+ {
92
+ if (OpenTheTestData == false)
93
+ {
94
+ editOK = false;
95
+ OpenTheTestData = true;
96
+ foreach (GameObject Target in Targets)
97
+ {
98
+ Destroy(Target);
99
+ }
100
+ }
101
+ else
102
+ {
103
+ editOK = true;
104
+ OpenTheTestData = false;
105
+ for(int i = 0;i <= 100; i++)
106
+ {
107
+ if (Destroyed[i] == false)
108
+ {
109
+ GameObject Prefab = Instantiate(RayScript.DataObj[IDData[i]], PositionData[i], RotationData[i]) as GameObject;
110
+ Prefab.name = Convert.ToString(i);
111
+ Targets.Insert(i, Prefab);
112
+ }
113
+ else
114
+ {
115
+ Targets.Insert(i, null);
116
+ }
117
+ }
118
+ }
119
+ }
120
+ //Save-----------------------------------------------------
121
+ if (Input.GetKeyDown("s"))
122
+ {
123
+ JSONvar vardatas = new JSONvar();
124
+ vardatas.IDData = IDData;
125
+ vardatas.PositionData = PositionData;
126
+ vardatas.RotationData = RotationData;
127
+ vardatas.Targets = Targets;
128
+ vardatas.Destroyed = Destroyed;
129
+ SaveData = JsonUtility.ToJson(vardatas);
130
+ Debug.Log(SaveData);
131
+ File.WriteAllText(DataPath, SaveData, System.Text.Encoding.UTF8);
132
+ }
133
+ if (Input.GetKeyDown("o"))
134
+ {
135
+ JSONvar roaddata = JsonUtility.FromJson<JSONvar>(SaveData);
136
+ IDData = roaddata.IDData;
137
+ PositionData = roaddata.PositionData;
138
+ RotationData = roaddata.RotationData;
139
+ Targets = roaddata.Targets;
140
+ Destroyed = roaddata.Destroyed;
141
+ }
142
+ //play----------------------------------------------------
143
+ if (Input.GetKeyDown("p") && actionnow == false)
144
+ {
145
+ if (play == false)
146
+ {
147
+ play = true;
148
+ stop = false;
149
+ resumenow = true;
150
+ nowtimeScale = 1;
151
+ playstart = true;
152
+ GameObject[] Spawners = GameObject.FindGameObjectsWithTag("Spawner");
153
+ foreach (GameObject Spawner in Spawners)
154
+ {
155
+ Spawner.GetComponent<Spawn>().Spawner();
156
+ }
157
+ }
158
+ else if (play == true)
159
+ {
160
+ ResetGame();
161
+ }
162
+ }
163
+ //menu-----------------------------------------------------------------------
164
+ if (Input.GetKeyDown("m"))
165
+ {
166
+ if (MenuObj.activeSelf)
167
+ {
168
+ MenuObj.SetActive(false);
169
+ hideCursor = true;
170
+ actionnow = false;
171
+ }
172
+ else
173
+ {
174
+ MenuObj.SetActive(true);
175
+ showCursor = true;
176
+ actionnow = true;
177
+ }
178
+ }
179
+ if (play)
180
+ {
181
+ Pkey_text.text = "中断";
182
+ }
183
+ else
184
+ {
185
+ Pkey_text.text = "スタート";
186
+ }
187
+ if (stopnow && play == true)
188
+ {
189
+ stopnow = false;
190
+ Time.timeScale = 0;
191
+ }
192
+ else if (resumenow)
193
+ {
194
+ resumenow = false;
195
+ Time.timeScale = nowtimeScale;
196
+ }
197
+ if (showCursor == true)
198
+ {
199
+ Cursor.visible = true;
200
+ Cursor.lockState = CursorLockMode.None;
201
+ locking = true;
202
+ showCursor = false;
203
+ }
204
+ else if (hideCursor == true)
205
+ {
206
+ Cursor.visible = false;
207
+ Cursor.lockState = CursorLockMode.Locked;
208
+ locking = false;
209
+ hideCursor = false;
210
+ }
211
+ if(GameOver == true)
212
+ {
213
+ FAILED.SetActive(true);
214
+ Controller.GameOver = true;
215
+ Controller.showCursor = true;
216
+ Controller.actionnow = true;
217
+ FAILEDInfoText.text = GameOverLog;
218
+ }
219
+ }
220
+ public void RetryButton()
221
+ {
222
+ FAILED.SetActive(false);
223
+ CLEAR.SetActive(false);
224
+ ResetGame();
225
+ hideCursor = true;
226
+ GameOver = false;
227
+ actionnow = false;
228
+ Clear = false;
229
+ TotalBlocks = 0;
230
+ SphereID = 0;
231
+ }
232
+
233
+ void ResetGame()
234
+ {
235
+ play = false;
236
+ GameObject[] destroys = GameObject.FindGameObjectsWithTag("Player");
237
+ foreach (GameObject destroy in destroys)
238
+ {
239
+ Destroy(destroy);
240
+ }
241
+ GameObject[] Dummys = GameObject.FindGameObjectsWithTag("Dummy");
242
+ foreach (GameObject Dummy in Dummys)
243
+ {
244
+ Destroy(Dummy);
245
+ }
246
+ stop = false;
247
+ resumenow = true;
248
+ nowtimeScale = 1;
249
+ GameObject[] Spawners = GameObject.FindGameObjectsWithTag("Spawner");
250
+ foreach (GameObject Spawner in Spawners)
251
+ {
252
+ Spawner.GetComponent<Spawn>().Reset();
253
+ }
254
+ }
255
+
256
+ IEnumerator SwitchText()
257
+ {
258
+ while (true)
259
+ {
260
+ for (int i = 0; i <= Display_Data.Length - 1; i++)
261
+ {
262
+ Text_Source.SetText(Display_Data[i]);
263
+ yield return new WaitForSeconds(1f);
264
+ }
265
+ }
266
+ }
267
+ }
268
+
269
+ コード
270
+ ```
7
271
  エラーは
8
272
  ```ここに言語を入力
9
273
  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)'.

1

エラーコード加筆

2018/07/27 14:20

投稿

UDON3
UDON3

スコア80

title CHANGED
File without changes
body CHANGED
@@ -4,5 +4,18 @@
4
4
  private string DetaPath = Application.dataPath + "/original_data.txt";
5
5
  ```
6
6
  このパス指定のコードを追加した際にエラーが出ました。
7
+ エラーは
8
+ ```ここに言語を入力
9
+ 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)'.
10
+ See "Script Serialization" page in the Unity Manual for further details.
11
+ Controller..ctor () (at Assets/Scouse/Controller.cs:54)
12
+ ```
13
+
14
+ ```ここに言語を入力
15
+ 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)'.
16
+ See "Script Serialization" page in the Unity Manual for further details.
17
+ Controller..ctor () (at Assets/Scouse/Controller.cs:54)
18
+ ```
19
+ が何度も出てきます
7
20
  書き方に問題があるはずなのですが、何がいけないんでしょうか。
8
21
  分かる方が見たらすぐわかると思いますが、数時間調べてもわかりませんでした、誰か回答お願いします。