StreamingAssetPath宣言タイミングの問題
using UnityEngine; using System.Collections; public class createfolder : MonoBehaviour { string streamingassetpath = Application.streamingAssetsPath; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } }
クラス定義のすぐ下にStreamingAssetPathを定義しようとしたら
get_streamingAssetsPath is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'createfolder' on game object 'GameObject'. See "Script Serialization" page in the Unity Manual for further details. UnityEngine.Application:get_streamingAssetsPath() createfolder:.ctor() UnityEditorInternal.InternalEditorUtility:HierarchyWindowDrag(HierarchyProperty, Boolean, HierarchyDropMode) UnityEditor.DockArea:OnGUI()
というエラーを吐く。
Awake()かStart()内で記述しろというものなので以下のように修正する
using UnityEngine; using System.Collections; public class createfolder : MonoBehaviour { string streamingassetpath = ""; // Use this for initialization void Start() { streamingassetpath = Application.streamingAssetsPath; } // Update is called once per frame void Update () { } }
このように空のStringを宣言しておいて、Start()内で代入すればエラーが起きない。
ちなみに、「Application.persistentDataPath」の宣言でも同じ問題が起きるので、同じように対処すればOK