• 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 /
  • Help Room /
avatar image
0
Question by karma0413 · Dec 13, 2017 at 03:05 AM · classescloneinitializationdefinition

Cannot convert type (class) to the same type (class)

SUMMARIZING MY MAIN QUESTION: "In my Save/Load script, how the heck can I define and initialize a class called GameSettings, to be exactly like the GameSettings (class) located in my GameControllerScript.cs

Lets say I have my main GameControllerScript.cs Inside of it, is a large class called GameSettings; and inside of the class GameSettings, are a couple other classes like: AudioSettings and VideoSettings.

GameControllerScripts.cs

 [System.Serializable]
     public class GameSettings
     {
 
         [System.Serializable]
         public class PlayerControls
         {
             public bool controlIsKeyboard = true;
             public int controlNumber = 1;
         }
         public PlayerControls[] _playerControls = new PlayerControls[4];
 
         [System.Serializable]
         public class PlayerKeys
         {
             public string horizontalKey = "";
             public string verticalKey = "";
             public string aButton = "";
             public string bButton = "";
             public string xButton = "";
             public string yButton = "";
         }
         public PlayerKeys[] _playerKeys;
 
 
         [System.Serializable]
         public class AudioSettings
         {
             public bool soundEnbaled = true;
             public float soundVolume = 1f;
         }
         public AudioSettings _audioSettings;
 
 
         public class VideoSettings
         {
             public bool enabledAmbientOcclusion = true;
             public bool enabledMSAA = true;
         }
         public VideoSettings _videoSettings;
 
     }
     public GameSettings _gameSettings;


OKAY, Now I have a SAVE/LOAD script that needs to save that entire GameSettings class. (FYI: I am following the persisting data tutorial on Unity)

SaveSettingsScript.cs

   public GameControllerScript _controllerScript;
   public class GameSettings { }
     
 
 
     // Use this for initialization
     void Start()
     {
         
         GameSettings _gameSettings = ????????????
 
 
 
     }

AND THE SAVE FUNCTION (in the same script)

  public void Save()
     {
 
         //the thing that savaes for us
         BinaryFormatter bf = new BinaryFormatter();
         //file creating
         FileStream file = File.Create(Application.persistentDataPath + "/settingsInfo.dat");
         // What data we are gonna put in here
         // we need a clean class, NO MONOBEHAVIORS
         GameSettings data = _gameControllerScript._gameSettings; // ??? I should be able to to make the variable data to an exact copy of class:GameSettings _gameSettings inside of _gameController??
 
 
         //data.health = 10f; // or my local health
         //data.experience = 0f;
 
         bf.Serialize(file, data);
         file.Close();
     }




Comment
Add comment · Show 1
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 karma0413 · Dec 13, 2017 at 03:24 AM 0
Share

Obviously, I could go into the SaveSettingScript.cs and literally define the class:GameSettings to have all the sub-class definitions inside it...

but, isn't there like a more simple way to do this? something like...

this.gameObject._gameSettings = _gameControllerScript._gameSettings.GetType();

1 Reply

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

Answer by karma0413 · Dec 13, 2017 at 03:40 AM

Pretty Sure this fixed it Basically, the answer is here on line 10: You can define a class by referencing the Monobehavior class First and then the sub-class after it. Just as you would with variables, you can reference entire classes this same way.

Thank goodness I kept plugging away at it.

  public void Save()
     {
 
         //the thing that savaes for us
         BinaryFormatter bf = new BinaryFormatter();
         //file creating
         FileStream file = File.Create(Application.persistentDataPath + "/settingsInfo.dat");
         // What data we are gonna put in here
         // we need a clean class, NO MONOBEHAVIORS
         GameControllerScript.GameSettings data = _controllerScript._gameSettings;
         
         //data.health = 10f; // or my local health
         //data.experience = 0f;
 
         bf.Serialize(file, data);
         file.Close();
     }

Comment
Add comment · 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

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

71 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How can you load next the level after you destroy the last clone c# 1 Answer

Instantiate question 0 Answers

Fitting an instantiated prefab inside a cube? 0 Answers

Passing variables from one script to all clones 1 Answer

Arrays with multiple data types C# 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