Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question StreamWriter not creating files in Build!

Discussion in 'Scripting' started by Magenta-, Mar 2, 2023.

  1. Magenta-

    Magenta-

    Joined:
    Aug 25, 2022
    Posts:
    2
    EDIT:

    I fixed the issue by using
    Application.streamingAssetsPath
    instead of dataPath. This creates a folder inside the Build location without access issues.

    Hello!

    I'm running into an issue with the StreamWriter. I'm trying to create a disease simulation inside Unity, once the simulation has run I try collecting the data gathered under the simulation, such as infection positions and infections over time, and then send it to .CSV and .TXT files through a script.

    However, when I build my project and run the Executable I get the following error:

    upload_2023-3-2_16-15-41.png

    Here is the script in question:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5. using UnityEngine.UI;
    6. using System.IO;
    7. using System;
    8. using Unity.VisualScripting;
    9.  
    10. public class DataManager : MonoBehaviour
    11. {
    12.     [SerializeField] VarManager VarManager;
    13.     [SerializeField] Timer Timer;
    14.     [SerializeField] VarHolder VarHolder;
    15.     public int totInfected;
    16.  
    17.     string mainPath;
    18.  
    19.     // List for total Infections + Timestamp in seconds
    20.     public List<Tuple<int, int>> InfectionOverTimeS = new List<Tuple<int, int>>();
    21.  
    22.     public List<Vector2> InfectionLocations = new List<Vector2>();
    23.  
    24.     private void Awake()
    25.     {
    26.         VarManager = GameObject.Find("GameManager").GetComponent<VarManager>();
    27.         Timer = GameObject.Find("GameManager").GetComponent<Timer>();
    28.     }
    29.  
    30.     void Start()
    31.     {
    32.         totInfected = VarManager.totalInfected;
    33.  
    34.         InfectionLocations.Add(GameObject.Find("corner1").transform.position);
    35.         InfectionLocations.Add(GameObject.Find("corner2").transform.position);
    36.         Debug.Log("InfectionLocationsStart:");
    37.         Debug.Log("Found corner1: " + InfectionLocations[0]);
    38.         Debug.Log("Found corner2: " + InfectionLocations[1]);
    39.  
    40.         mainPath = Application.dataPath + @"\Simulation_Data_Log\";
    41.     }
    42.  
    43.     void Update()
    44.     {
    45.         totInfected = VarManager.totalInfected;
    46.  
    47.         if(!Timer.timerIsRunning)
    48.         {
    49.             DataCollection();
    50.         }
    51.     }
    52.  
    53.     private void OnDestroy()
    54.     {
    55.         //DataCollection();
    56.     }
    57.  
    58.     //Function for creating and storing all Data that's been gathered in the Sim
    59.     void DataCollection()
    60.     {
    61.         if(mainPath == null)
    62.         {
    63.             Directory.CreateDirectory(mainPath);
    64.         }
    65.         //InfectionOverTime Collection
    66.         if (InfectionOverTimeS.Count > 0)
    67.         {
    68.             using (StreamWriter sw = new StreamWriter(mainPath + "InfectionOverTime.csv"))
    69.             {
    70.                 sw.WriteLine("INFECTION OVER TIME - LOG" + "|" + " Total time(s):" + Timer.secondsPassed + " TOTAL INFECTIONS:" + totInfected + ",");
    71.                 sw.WriteLine("Time, Total Infections");
    72.                 sw.WriteLine("(0," + VarHolder.infectedPawnAmount + ")");
    73.                 for (int i = 0; i < InfectionOverTimeS.Count; i++)
    74.                 {
    75.                     string s = InfectionOverTimeS[i].ToString();
    76.                     s.Replace("(", "");
    77.                     s.Replace(")", "");
    78.                     sw.WriteLine(s);
    79.                 }
    80.                 Debug.Log("Infection Over Time Finished Collecting!");
    81.             }
    82.         }
    83.  
    84.         if (InfectionLocations.Count > 0)
    85.         {
    86.             using (StreamWriter sw = new StreamWriter(mainPath + "InfectionLocations.csv"))
    87.             {
    88.                 sw.WriteLine("INFECTION LOCATIONS - LOG" + "|" + " Total time(s):" + Timer.secondsPassed + "TOTAL INFECTIONS:" + totInfected + ",");
    89.                 sw.WriteLine("X-Position, Y-Position");
    90.                 for (int i = 0; i < InfectionLocations.Count; i++)
    91.                 {
    92.                     string s = InfectionLocations[i].ToString();
    93.                     s.Replace("(", "");
    94.                     s.Replace(")", "");
    95.                     sw.WriteLine(s);
    96.                 }
    97.                 Debug.Log("Infection Locations Finished Collecting!");
    98.             }
    99.         }
    100.  
    101.         //TXT doc with all the simulation variables
    102.         using (StreamWriter sw = new StreamWriter(mainPath + "SimulationVars.txt"))
    103.         {
    104.             sw.WriteLine("-|-Simulation Variables & Parameters TXT Document-|-");
    105.             sw.WriteLine("--------------------------------------------------------");
    106.             sw.WriteLine("This document contains all the variables and paramateres");
    107.             sw.WriteLine("set by the user for testing.");
    108.             sw.WriteLine("");
    109.             sw.WriteLine("DISEASE VICTIM SUSCEPTIBILITY | Minimum val:" + VarHolder.minimumSusceptibility + " | Maximum val:" + VarHolder.maximumSusceptibility);
    110.             sw.WriteLine("MASK WEARING PROBABILITY | Percentage:" + VarHolder.hasMaskChance);
    111.             sw.WriteLine("BREATH RADIUS | Units:" + VarHolder.breathRadius);
    112.             sw.WriteLine("BREATH INFECTION CHANCE | Percentage:" + VarHolder.areaInfectionChance);
    113.             sw.WriteLine("COUGH CHANCE | Percentage:" + VarHolder.coughChance);
    114.             sw.WriteLine("SNEEZE CHANCE | Percentage:" + VarHolder.sneezeChance);
    115.             sw.WriteLine("AEROSOL CLOUD INFECTION CHANCE | Percentage:" + VarHolder.cloudInfectionChance);
    116.             sw.WriteLine("--------------------------------------------------------");
    117.             sw.WriteLine("TOTAL SIMULATION TIME | Hours:" + VarHolder.simHours + " Minutes:" + VarHolder.simMinutes);
    118.             sw.WriteLine("TOTAL PAWNS IN PLAY:" + VarManager.totalPawns);
    119.             sw.WriteLine("UN-INFECTED:" + VarHolder.uninfectedPawnAmount);
    120.             sw.WriteLine("INFECTED:" + VarHolder.infectedPawnAmount);
    121.             sw.WriteLine("VACCINATED:" + VarHolder.vaccinatedPawnAmount);
    122.             sw.WriteLine("");
    123.             sw.WriteLine("Percentage of Infected at Start: " + Convert.ToInt32((VarManager.infectedPawnAmount / VarManager.totalPawns) * 100) + "%");
    124.             sw.WriteLine("Percentage of Vaccinated at Start: " + Convert.ToInt32((VarManager.vaccinatedPawnAmount / VarManager.totalPawns) * 100) + "%");
    125.         }
    126.  
    127.     }
    128. }
    129.  
     

    Attached Files:

    Last edited: Mar 2, 2023
  2. SF_FrankvHoof

    SF_FrankvHoof

    Joined:
    Apr 1, 2022
    Posts:
    780
  3. Magenta-

    Magenta-

    Joined:
    Aug 25, 2022
    Posts:
    2
    So the file I write to has to be located at a 'persistant' path somewhere else on the Users PC? Is there no way to have it generate inside the Build folder with PersistentDataPath?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    There isn't really a build folder... it's more just sort of a database of your built assets. Depending on which target you build for, it might not even really exist, such as WebGL, or Android.

    But all targets have SOME way of storing persistent data, and that path (as mentioned above) gets you to it.