Help with static variables not being saved

I made a script where all my scripts are written, I access them periodically so I don’t have to write the same script over and over. Now I have a problem when I set a variable and read it again its ok, but when I set it and then go back to read it, its not set. I like to have a place where I can keep all my scripts that I need to access again and again from different scripts.

using UnityEngine;
using System.Collections;

public class MyScript : MonoBehaviour {

public static void setupGridList (int type, int g, int s, int p, int m, int b, int a, int grid) {
		int [,,,, ] planetLocation    = new int [10,10,10   ,27,7];
		int [,,,,,] satelliteLocation = new int [10,10,10,10,27,7];

		if (type == 0) { // 0 = New Game / Restart
		// Resets Planets 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 (b = 0; b <=26; b++){ // GridBox (b)
						for (a = 0; a <=6; a++){ // Area (a)
								planetLocation[g,s,p,b,a] = 0;
						}}}}}
		// Resets Satellites 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)
									satelliteLocation[g,s,p,m,b,a] = 0;
							}}}}}}
			print("New Grid");
		}
		if (type == 1) { // 1 = Write to Grid

			satelliteLocation[g,s,p,m,b,a] = grid;
			print("Wrote #" + satelliteLocation[g,s,p,m,b,a]);
		}

		if (type == 2) { // 1 = Read Grid
			print ("Read #" + satelliteLocation[g,s,p,m,b,a]);
		}}}

using UnityEngine;
using System.Collections;

public class Setup : MonoBehaviour {

	void Start () {
		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
		MyScript.setupGridList(2,1,4,3,1,1,1,0); // 2 = Read from Grid Location
}

When I print in console my results are;
New Grid
Wrote #5
Read #0

So Read #0 should be 5, I went from Formatting, to Writing 5 at that location and it should read and stay 5 not revert back to 0 at that location. Am I doing something wrong? The script MyScript is not attached to anything, it is just there to hold all my scripts to access.

planetLocation (and satelliteLocation) are just local variables. Normal programming rules say they’re deleted and remade each time setupGridList is run. In fact, you can see how your code deliberately remakes them from scratch in the 1st two lines! If they need to stick around, make the “globals.”

I prefer putting a non-static class on an empty game object. But, if you want to use the “not on a GO, all static funcs” method: 1) move the two arrays to above setupGrid, as: public static int [.... 2) Put the new int part so you only do it once, during the “format” step.

Ot just look up some examples of static functions and data here. It’s a semi-common trick.