[Solved] Write to text files with c# in Unity3D using StreamWriter

StreamWriter is very simple tool which let you to write your data into text files.This script is very easy to understand and working method which I have implemented in my Foxes and Rabbit Simulation.Unity3D C#

using UnityEngine;
using System.Collections;
using System.IO;

public class countfox : MonoBehaviour {
public int count_fox;
StreamWriter sw;
GameObject [] fox;

void Start(){
sw = new StreamWriter(“count_fox.txt”);   //The file is created or Overwritten outside the Assests Folder.
}

void Update(){

fox = GameObject.FindGameObjectsWithTag(“fox”);
count_fox= fox.Length;

sw.WriteLine(” ” + count_fox+” “+System.DateTime.Now, true);

sw.Flush();

}

}

2 comments on “[Solved] Write to text files with c# in Unity3D using StreamWriter”

  1. Eric says:

    Hi,
    I’ve tried your code. The first time worked just fine. But the rest of the time I kept receiving access to the path is denied report. Did you get this? Or do you have any idea how to fix this?

Leave a Reply