Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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.

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

Why Original GameObject Destroy Clones ? 0 Answers

Stop Infinitely Spawning Object on Javascript 1 Answer

Not being able to destroy clone of prefab. 0 Answers

Clones not using animator and enabling scripts? 1 Answer

(SOLVED) Accessing (string) arrays by enumerations from another class 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