Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

StreamWriter not working

Discussion in 'Scripting' started by FyreDogStudios, Nov 24, 2015.

  1. FyreDogStudios

    FyreDogStudios

    Joined:
    Aug 23, 2015
    Posts:
    97
    I have a script I am working on that just saves the r,g,b,a values of an array of colours to a txt document, this is the code I have, can someone point me to why it is not working, my issue is the text file is created, but turns up empty. Cheers.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.IO;
    4.  
    5. public class ColourPalleteCurt : MonoBehaviour {
    6.  
    7.     [System.Serializable]
    8.     public struct Pallete {
    9.         public string palleteName;
    10.         public Color[] colours;
    11.     }
    12.    
    13.     public Pallete[] palletes;
    14.  
    15.     public void SavePallete(Pallete toSave, string path){
    16.         StreamWriter write = new StreamWriter (path + toSave.palleteName + ".txt");
    17.  
    18.         for(int i = 0; i < toSave.colours.Length; i++){
    19.             write.WriteLine(toSave.colours[i].r.ToString() + " " + toSave.colours[i].g.ToString() + " " +  toSave.colours[i].b.ToString()
    20.                             + " " + toSave.colours[i].a.ToString());
    21.         }
    22.  
    23.     }
    24.  
    25.     public void Start(){
    26.         SavePallete (palletes[0], Application.persistentDataPath);
    27.     }
    28. }
    29.  
     
  2. FyreDogStudios

    FyreDogStudios

    Joined:
    Aug 23, 2015
    Posts:
    97
    There are colours in the array too, I did check that. The palette also has a name so that isn't the problem either.
     
  3. AlucardJay

    AlucardJay

    Joined:
    May 28, 2012
    Posts:
    328
    Code (csharp):
    1. StreamWriter sw = new StreamWriter( filePath );
    2. sw.WriteLine( someData );
    3. sw.Flush();
    4. sw.Close();
    You flush it when you want the data to be persisted. Until that point you are just filling a stream in memory with data; Flush actually causes that data to be written to disk. : MSDN flush

    However the stream will remain open; accessing it again may cause an unexpected exception. : MSDN close
     
  4. FyreDogStudios

    FyreDogStudios

    Joined:
    Aug 23, 2015
    Posts:
    97
    Of course, I forgot about those. Feels so stupid! Cheers alcardj!