Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Maranasa · Jun 28, 2014 at 04:21 PM · saveloadstatic

Save Publict Static Variables

I have a very large Class of public static variables (used for A customizable spaship the player can build) and they are not in an array.

I need to save these variables so that the player doesn't have to rebuild the ship every time they exit and restart the game.

I am very confused about this. I just want a simple way to write the value of all the variable in the class to a file, and also to load the file to reset the variable back to what is saved. Here is a (VERY) small snip-it of the class I want to save.

(keep in mind this class has thousands of variables in this class so I need a way to save that will not require me to write a line of code for each and every variable)

(also C# please!!!)

//The parts that make up the players ship

public class Ship

{

 public static GameObject hul;

 public static GameObject aet1hul;

 public static GameObject inp1hul;

 public static GameObject shi1hul;

 public static GameObject win1hul;
 public static GameObject win2hul;
 public static GameObject win3hul;
 public static GameObject win4hul;
 public static GameObject win5hul;
 public static GameObject win6hul;
 public static GameObject win7hul;
 public static GameObject win8hul;

 public static GameObject aug1hul;
 public static GameObject aug2hul;
 public static GameObject aug3hul;
 public static GameObject aug4hul;
 public static GameObject aug5hul;
 public static GameObject aug6hul;
 public static GameObject aug7hul;
 public static GameObject aug8hul;

  //...ect...ect...ect...


}

Comment
Add comment · Show 6
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 KiraSensei · Jun 28, 2014 at 04:24 PM 0
Share

The first thing I'm thinking of is THIS, but it will not work for game objects, only basic variables like int, double or string. So if the ship is a combination of limited elements, you can save them with int vars, and map them to an enum for example.

avatar image Maranasa · Jun 28, 2014 at 04:34 PM 0
Share

Is there a way then to record the entire script in its current form for reloading?

avatar image KiraSensei · Jun 28, 2014 at 04:36 PM 0
Share

Why would you want to record an entire script ? It will not record the value of the variables.

avatar image Maranasa · Jun 28, 2014 at 04:45 PM 0
Share

I was hoping there is a way to record it with it's current changes. including what the variables current value is. I am rather new at this...

avatar image Lo0NuhtiK · Jun 28, 2014 at 04:45 PM 0
Share

PlayerPrefs.SaveAll$$anonymous$$yStuffTheWayIwantIt() ;

...see if that works. If it doesn't, you're going to need to make your own save/load method(s). Use arrays, also, ins$$anonymous$$d of making numbered variables like you have it. GameObject[] augHul = new GameObject[8] ; would be a lot better than 8 different variables with their index numbers included in their names. Same goes for GameObject[] winHul ; and whatever other of the not-shown variables you mention that could be thrown together into arrays. Then you'd just loop through them to do your save/load stuff ins$$anonymous$$d of writing thousands upon thousands of save/load lines for thousands of variables.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by frarees · Jun 28, 2014 at 04:58 PM

Unity won't serialize static variables on your scripts, as stated here. I recommend you to follow a singleton pattern on that behaviors (e.g. this) to save all your data or use the PlayerPrefs class to store it instead.

Comment
Add comment · Show 1 · 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 ExtraCat · Jun 27, 2021 at 03:30 PM 0
Share

alas the blog link is dead

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

6 People are following this question.

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

Related Questions

Saving and Loading with a static or a local function 0 Answers

Remembering the state of a scene 1 Answer

How do I open a PC project/scene on MAC? 1 Answer

Save and Load Questions 0 Answers

Null Reference Exception even after check 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