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 maxclark86 · Feb 20, 2015 at 12:53 AM · c#serializationcustom class

Making a Serializable class that doesn't throw "The class named 'Legacy' is not derived from MonoBehaviour or ScriptableObject!"

I have the functionality I want. My issue is that I'm getting the warning "The class named 'Legacy' is not derived from MonoBehaviour or ScriptableObject!" I want the warning to go away. It is an accurate warning, because my class, Legacy, does not extend Monobehaviour. I did this so that I may tag it as [Serializable]. I'm trying to learn about game saves and serialization from this Unity live training video.

The difference I can see between my code the trainer is that my Legacy class is in a separate file and I instantiate an array of type Legacy from my GameManager script, which does extend Monobehaviour. In the live training, the class that is serialized is a private class and he does not get this error.

Here is my Legacy class:

 [System.Serializable]
 public class Legacy {
     
     public int id;
     public string name;
     public int fame;
 
     public Legacy( int id, string name, int fame) {
         this.id = id;
         this.name = name;
         this.fame = fame;
     }
 
     public void AddToLegacyManager() {
         LegacyManager.legMan.legacies[id] = this;
     }
 }
 

And here is where I hardcode a few elements (within my a class derived from Monobehaviour):

 void CreateLegacies() {
 
         //Create legacy for each player
         Legacy leg1 = new Legacy( 0, "Achilles", 4);
         Legacy leg2 = new Legacy( 1, "Hercules", 2);
 
         leg1.AddToLegacyManager();
         leg2.AddToLegacyManager();
     }


And here is where I serialize in LegacyManager:

     public void Save() {
         BinaryFormatter bf = new BinaryFormatter();
 
         FileStream file = File.Create( Application.persistentDataPath + "/playerInfo.dat");
         
         Legacy[] legs = new Legacy[2];
         legs[0] = legacies[0];
         legs[1] = legacies[1];
 
         bf.Serialize( file, legs);
         file.Close();
         
     }
 
     public void Load() {
         BinaryFormatter bf = new BinaryFormatter();

         if( File.Exists( path + "/playerInfo.dat")) {
             FileStream file = File.Open( Application.persistentDataPath + "/playerInfo.dat", FileMode.Open);

             Legacy[] legs = (Legacy[])bf.Deserialize( file);
             file.Close();
 
             legacies[0] = legs[0];
             legacies[1] = legs[1];
         }
     }
 }

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

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.

Follow this Question

Answers Answers and Comments

20 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

Related Questions

How can I get functionality similar to OnValidate for a custom class? 4 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Instantiating a serializable class and variable 1 Answer

C# how to setup a Binary Serialization 4 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