• 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
Question by Sarrivin · Sep 01, 2016 at 05:46 AM · c#staticdatascenespersistence

Most convenient and fast way to persist/access/modify many values from scene to scene? (C#)

I looked for answers, but still at loss here. I have some character generator, which creates then saves player character variables. They are saved into a fresh script "PlayerCharacterData", which is a component of object CharacterData. This object has DontDestroyOnLoad, so it persists to other scenes. After generating character, game loads character data into that script, and when I switch scenes, data is properly loaded.

But now I have this core and I need my numerous UI objects to load data from "PlayerCharacterData" class, so UI can fill all fields, etc. Later whole game will be depending on variable values from "PlayerCharacterData".

This is part of my PlayerCharacterData script:

 public class PlayerCharacterData : MonoBehaviour {

       void Awake(){
           DontDestroyOnLoad (this); // keeps the object when changing scenes
    }
 
    // below we have data stored in variables. It was loaded by another script.    
    public string characterName;
    public int characterSpeed;
    public List<Weapon>characterInvWeapon;
    // etc, more variables x50-70
    }

Now here's example of my public class UIPlayerCharacterData. I added a tag to object CharacterData in Unity Editor to make "Find" faster:

 public class UIPlayerCharacterData : PlayerCharacterData {
 
     public void NameToCharacterDataUI() {
        // this would be required to make it work: PlayerCharacterData playerCharacterData = GameObject.FindGameObjectWithTag("CharacterData").GetComponent<PlayerCharacterData>();
        Text_ch_sh_char_name_string.text = playerCharacterData.characterName;
    }
    // etc, more functions like that x50-70    
 
    void Awake () {
       PlayerCharacterData playerCharacterData = GameObject.FindGameObjectWithTag("CharacterData").GetComponent<PlayerCharacterData>();
       NameToCharacterDataUI();
       // etc, calling more functions x50-70 
    }
 }

Problem is, those classes are on different objects. First is component of CharacterData object which persists from 1st scene, second is component of major UI panel in 2nd scene. Second class has LOTS of UI fields to fill, just showed you one of those. Each takes data from first class (component on CharacterData object). So there's like 50-70 variables for UI to pull data from.

And that's just a start, because whole game will need to get and modify data in PlayerCharacterData script.

Now, I've found out the function NameToCharacterData() isn't working, because it has no reference in playerCharacterSheet variable: "Object reference not set to an instance of an object". Thought it was sorted out in Awake() - I was wrong. So seems we'd need to set PlayerCharacterData playerCharacterData = GameObject.Find("CharacterData").GetComponent<PlayerCharacterData>();

...in every of 50-70 functions filling the fields in UI. Not to mention every other system in game would need to do that too. Plus, everytime player opens the UI those UI fields need to be re-generated.

Is there some faster, more convenient way to do it? I was thinking about making all PlayerCharacterData variables static - this would be fast as lightning, but I'd prefer to make our engine ready for multiplayer (between 1 and 200 players). So not sure.

Also, wouldn't it be better to save character data variables into some script without Monobehaviour, so not connected to a gameObject, and maybe then we could just use PlayerCharacterData playerCharacterData = new PlayerCharacterData(); to get values from that class? I simply need those values accesible really fast and from everywhere.

Really considering the use of static variables at this point, but if there's a better solution, or if there's a better way create static variables for a multiplayer game, let me know.

Comment

People who like this

0 Show 0
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

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Persistence of saving data not working 2 Answers

How to pass data through scenes? 0 Answers

Transfer data between scenes + saving it C# 1 Answer

Multiple Cars not working 1 Answer

using a static list between scenes 0 Answers


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