- Home /
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.
Answer by Owen-Reynolds · May 17, 2014 at 02:16 PM
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.
I would of moved the arrays above, but it will be ignored inside the static class, but I can see now why it was reverted. I can't put it outside or inside if type = 0, im bummed out. If I make it non static it does not work when I use $$anonymous$$yScript.setupGridList(1,1,4,3,1,1,1,5); saying an object reference is required to access non static member.
Ok, got it, I added this to the beginning, didn't see it at first, had to read it like 4 times until it kicked in my head.
using UnityEngine;
using System.Collections;
public class $$anonymous$$yScript : $$anonymous$$onoBehaviour {
public static int [,,,, ] planetLocation = new int [10,10,10 ,27,7];
public static int [,,,,,] satelliteLocation = new int [10,10,10,10,27,7];
}
$$anonymous$$nowing this will sure help a lot for my other codes I had problem with. Thx.