How do you save, read, edit an Arraylist (int).

I’ve got this integer array, already tried different methods I found on net, I only get permission issues.

Here is my array

public static int [,,,,,] gridLocation = new int [10,10,10,10,27,7];
public static string savePath;

Here I read and write to my gridLocation, each letter represent a number.

gridLocation[g,s,p,m,b,a] = 4;
print ("Read #" + gridLocation[g,s,p,m,b,a]);  // results will be 4.

Here is my savegame path

var values = System.Enum.GetValues( typeof( System.Environment.SpecialFolder ));
savePath = System.Environment.GetFolderPath((System.Environment.SpecialFolder)values.GetValue(3));
print(savePath + "/Beyond.sav"); // results will be /Users/yourname/Beyond.sav

I would like to create a new save file, and once created - when 1 int is changed then only this int will be edited. And when opening the game this game save will be loading into the game and editing will continue during game save.

Fixed up code from comments, just to keep the answer clean. Hasn’t been tested, but it throws no errors in the Unity editor. You’ll have to figure out any further issues yourself. (challenge!)

using UnityEngine;
using System;
using System.IO;
using System.Linq;
using System.Collections;
using System.Collections.Generic;

public class SaveData : MonoBehaviour
{
	// Store save directory and filename
	private string path = "Save";
	private string username = "David";
	private static int saveListIdTotal = 32; // There will be a list of 32 numbers
	// Save list of ints
	private List<int> saveList = new List<int>(SaveData.saveListIdTotal);

	void Start()
	{
		for(int i = 5; i <= 10; ++i) saveList.Add(i); // Replaced random number, could not fix it.
	}
	
	void Update()
	{
		if(Input.GetKey(KeyCode.S)) saveGame(username);
		if(Input.GetKey(KeyCode.L)) loadGame(username);
		if(Input.GetKey(KeyCode.D)) deleteGame(username);
	}
	
	private void deleteGame(string filename) // Delete file
	{
		if(File.Exists(path + filename)) {
			File.Delete(path + filename);
			Debug.Log("File deleted");
		}
		else Debug.Log("File doesn't exists");
	}
	
	private void loadGame(string filename)
	{
		if(File.Exists(path + filename)) { // If file exists, load file contents and parse into int list
			string[] fileContents = File.ReadAllLines(path + filename); 
			foreach(string i in fileContents) {
				this.saveList.Add(Int32.Parse(i));
			}
			Debug.Log(fileContents);
		} else Debug.Log("File does not exist.");
	}
	
	private void saveGame(string filename)
	{
		// If the file exists,
		if(File.Exists(path + filename)) {
			// Convert int list to comma separated string
			string filePath = "" + this.path + "" + filename + ""; // Safeguarding cause I didn't feel like making sure
			string[] contents = new string[1];
			contents[0] = string.Join(",", saveList.Select(i => i.ToString()).ToArray());
			File.WriteAllLines("" + filePath, contents);
		}
		else {
			// If it doesn't exists, create the file and re-run
			File.Create(path + filename);
			saveGame(filename);
		}
	}
}

OK got my multi dimensional list to format, save and read.

public class Setup : MonoBehaviour {
     MyScript.setupGridList(0,0,0,0,0,0,0,0); // 0 = Format Grid (New)
     MyScript.setupGridList(1,1,4,3,1,1,1,5); // 1 = Write to Grid Location (Save)
     MyScript.setupGridList(2,1,4,3,1,1,1,0); // 2 = Read from Grid Location (Load)
}

and the main coding area

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

public class MyScript : MonoBehaviour {

  public static void setupGridList (int type, int g, int s, int p, int m, int b, int a, int grid) {
	if (type == 0) {
		// Resets Grid List to 0
		for (g = 0; g <=9; g++){ // Galaxy (g)
			for (s = 0; s <=9; s++){ // Star (s)
				for (p = 0; p <=9; p++){ // Planet (p)
					for (m = 0; m <=9; m++){ // Satellite (m)
						for (b = 0; b <=26; b++){ // GridBox (b)
							for (a = 0; a <=6; a++){ // Area (a)
								gridLocation[g,s,p,m,b,a] = 0;
							}}}}}}}
	if (type == 1) { // 1 = Write to Grid
		gridLocation[g,s,p,m,b,a] = grid;
		var path = Application.dataPath + "/Save";
		if (!Directory.Exists (path)) Directory.CreateDirectory(path);
		using(StreamWriter sw = new StreamWriter(path + "/Beyond.txt")) foreach(var item in gridLocation) sw.WriteLine(item);
	}

	if (type == 2) { // 1 = Read Grid
		var path = Application.dataPath + "/Save";
		if (!Directory.Exists (path)) Directory.CreateDirectory(path);
		using(StreamReader sr = new StreamReader(path + "/Beyond.txt"))
		for (int gT = 0; gT <=9; gT++){ // Galaxy (g)
			for (int sT = 0; sT <=9; sT++){ // Star (s)
				for (int pT = 0; pT <=9; pT++){ // Planet (p)
					for (int mT = 0; mT <=9; mT++){ // Satellite (m)
						for (int bT = 0; bT <=26; bT++){ // GridBox (b)
							for (int aT = 0; aT <=6; aT++){ // Area (a)
								gridLocation[gT,sT,pT,mT,bT,aT] = int.Parse(sr.ReadLine());
		}}}}}}}}

For user data like name, resource amount etc I will use a different save since it will contain strings and integers, I will use the code from above with a little change. Thx for all your help.