- Home /
 
Write Data From List To CSV File
I am trying to write to a CSV file the values that I put in lists from a key press in the keyboard. All the values should be in the same place as they show in the lists. I believe this means to write the values as soon as they are added to the list. This is my code:
 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Text;
 using System.IO;
 using System;
  
 public class record : MonoBehaviour
 {
     public List<string> inventory = new List<string>();
     public List<string> OnlyX = new List<string>();
  
     void Update()
     {
         if (Input.GetKeyDown(KeyCode.F))
         {
             inventory.Add("F");
    
         }
    
         if (Input.GetKeyDown(KeyCode.X))
         {
             OnlyX.Add("X");
         }
  
         string filePath = getPath();
         StreamWriter writer = new StreamWriter(filePath);
         writer.WriteLine("Inventory,OnlyX");
  
         for (int i = 0; i < inventory.Count; ++i)
         {
             writer.WriteLine(inventory[i]);
         }
         for (int j = 0; j < OnlyX.Count; ++j)
         {
             writer.WriteLine("," + OnlyX[j]);
    
             }
    
         }
  
         writer.Flush();
         writer.Close();
     }
     private string getPath()
     {
 #if UNITY_EDITOR
         return Application.dataPath + "/Data/"  + "Saved_Inventory.csv";
 #elif UNITY_ANDROID
         return Application.persistentDataPath+"Saved_Inventory.csv";
 #elif UNITY_IPHONE
         return Application.persistentDataPath+"/"+"Saved_Inventory.csv";
 #else
         return Application.dataPath +"/"+"Saved_Inventory.csv";
 #endif
     }
 }
 
              
               Comment
              
 
               
              Your answer
 
             Follow this Question
Related Questions
List in prefab instantiated object not saving after Awake 0 Answers
A node in a childnode? 1 Answer
CSV Reader delimiter problem 1 Answer
C# find specific piece of data and where it is in list 1 Answer
how to set the value of my start time? 2 Answers