質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

Q&A

0回答

625閲覧

UnityでPlayer位置情報を書き出す方法

ryuseiyamaga

総合スコア4

C#

C#はマルチパラダイムプログラミング言語の1つで、命令形・宣言型・関数型・ジェネリック型・コンポーネント指向・オブジェクティブ指向のプログラミング開発すべてに対応しています。

Unity3D

Unity3Dは、ゲームや対話式の3Dアプリケーション、トレーニングシュミレーション、そして医学的・建築学的な技術を可視化する、商業用の開発プラットフォームです。

0グッド

0クリップ

投稿2019/11/22 15:58

unityでplayer位置を取得し、CSVファイルで書き出す

UnityでPlayerの位置情報を取得し、CSVファイルを書き出そうとしたのですが、
CSVファイルが見つからないようで以下のエラーメッセージが発生しました。

発生している問題・エラーメッセージ

DirectoryNotFoundException: Could not find a part of the path "C:\Users\yamag\Documents\chichubijutsukan\Assets\moving_log\2019-11-22-23-00-20.csv". System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.Boolean anonymous, System.IO.FileOptions options) (at <567df3e0919241ba98db88bec4c6696f>:0) System.IO.FileStream..ctor (System.String path, System.IO.FileMode mode, System.IO.FileAccess access, System.IO.FileShare share, System.Int32 bufferSize, System.IO.FileOptions options, System.String msgPath, System.Boolean bFromProxy, System.Boolean useLongPath, System.Boolean checkHost) (at <567df3e0919241ba98db88bec4c6696f>:0) (wrapper remoting-invoke-with-check) System.IO.FileStream..ctor(string,System.IO.FileMode,System.IO.FileAccess,System.IO.FileShare,int,System.IO.FileOptions,string,bool,bool,bool) System.IO.StreamWriter.CreateFile (System.String path, System.Boolean append, System.Boolean checkHost) (at <567df3e0919241ba98db88bec4c6696f>:0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize, System.Boolean checkHost) (at <567df3e0919241ba98db88bec4c6696f>:0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append, System.Text.Encoding encoding, System.Int32 bufferSize) (at <567df3e0919241ba98db88bec4c6696f>:0) System.IO.StreamWriter..ctor (System.String path, System.Boolean append) (at <567df3e0919241ba98db88bec4c6696f>:0) (wrapper remoting-invoke-with-check) System.IO.StreamWriter..ctor(string,bool)

該当のソースコード

C#

1using System.Collections; 2using System.Collections.Generic; 3using UnityEngine; 4 5using System.IO; 6using System; 7 8 9public class DataLogger : MonoBehaviour 10{ 11 private StreamWriter sw; 12 private FileInfo fi; 13 14 private DateTime date; 15 // Use this for initialization 16 void Start() 17 { 18 date = DateTime.Now; 19 } 20 21 // Update is called once per frame 22 void Update() 23 { 24 string format = "yyyy-MM-dd-HH-mm-ss"; 25 StreamWriter sw; 26 FileInfo fi; 27 Debug.Log(date.ToString(format)); //ログ表示、カッコ内に好きな文字列などを入れる 28 string filename = date.ToString(format); //ファイルネームの設定、今回は日時をもとにcsvファイルを設定 29 fi = new FileInfo(Application.dataPath + "/moving_log/" + filename + ".csv"); // Aplication.dataPath で プロジェクトファイルがある絶対パスが取り込める 30 //str = this.gameObject + "," + (transform.position.x) + "," + (transform.position.y) + "," + (transform.position.z) + "," + transform.rotation.x + "," + transform.rotation.y + "," + transform.rotation.z + "," + transform.rotation.w; 31 //sw = fi.AppendText(); 32 sw = fi.AppendText(); 33 string str = this.gameObject + "," + (transform.position.x) + "," + (transform.position.y) + "," 34 + (transform.position.z) + "," + transform.rotation.x + "," + transform.rotation.y + "," 35 + transform.rotation.z + "," + transform.rotation.w; 36 sw.WriteLine(str); 37 sw.Flush(); 38 sw.Close(); 39 } 40 41} 42

試したこと

上記のように書き込んだのですが、何が原因かわかりません。改善点を教えて下さい。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

YAmaGNZ

2019/11/22 23:32

書き込むフォルダは存在するのですか?
ryuseiyamaga

2019/11/23 00:20

はい、自身で作っております
sakura_hana

2019/11/23 06:28

「DirectoryNotFoundException」なのでフォルダが無いか開けないのが原因かなと思います。 フォルダが存在するか、フォルダ名の全ての文字が合っているか、書き込み権限があるかを確認してみてください。 なお開こうとしているファイル名には「このスクリプトが実行された時刻(秒まで)」が含まれています。 そんなドンピシャなファイル名のファイルが本当に自分で作れているかも疑問なので、一旦ファイル名を固定値にして試すといいと思います。
ryuseiyamaga

2019/11/23 06:30

わかりました、別名にして試してみます
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問