Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by d112570 · May 17, 2014 at 01:47 PM · getsetstatic-variables

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image d112570 · May 17, 2014 at 02:41 PM 0
Share

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.

avatar image d112570 · May 17, 2014 at 02:46 PM 0
Share

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.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How do I set and get the position of a object? 3 Answers

C# Parent-SubClass set inherited properties help 1 Answer

Getter Working, Setter not. 3 Answers

PlayerPref/PlayerPrefsX why not able to store boolean values? 1 Answer

problem with get{} and set{} 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges