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