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 /
This question was closed Aug 16, 2018 at 03:00 PM by nightmare3399 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by nightmare3399 · Aug 15, 2018 at 09:44 PM · prefabclassessave data

saving to prefab with classes from other scritps

I've created a simple class which uses another class from a different script:

 using UnityEngine;
 
 public class SimpleClass : MonoBehaviour {
 
     public DifferentScript.AnotherClass example;
     public int test = 0;

 }

I then used another script to create and save an empty GameObject with this "SimpleClass" to my asset folder as a prefab.

 public class SaveObject : EditorWindow {
 
     public static void SavePrefab(GameObject item, string path)
     {
         Object prefab = PrefabUtility.CreatePrefab(path, item);
     }
 }

&

 using UnityEngine;
 
 public class SavingScript : MonoBehaviour {
 
     public GameObject objectWithSimpleClass;
 
     void Start () {
         save();
     }
     void save()
     {
         GameObject obj = Instantiate(objectWithSimpleClass);
         obj.GetComponent<SimpleClass>().examle = randomdata;
         obj.GetComponent<SimpleClass>().test = 1;
         SaveObject.SavePrefab(obj, path);
     }
     
 }

The Prefab is saved without errors, and the int test is equal to 1, just like i set it in the save() function. However "example" is set to null and does not save "randomdata". How do i fix this?,

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

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by nightmare3399 · Aug 16, 2018 at 02:59 PM

I've found the solution: you can't save custom classes in this way by default, as they are not "serialised". To be able to save them you need to serialise them first. https://docs.unity3d.com/Manual/script-Serialization.html https://docs.unity3d.com/ScriptReference/Serializable.html

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

Follow this Question

Answers Answers and Comments

115 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 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

Classes VS Variable Scripts 1 Answer

Editor Script for selecting class property for saving 1 Answer

How do I find a Prefab after I Replaced said Prefab? 1 Answer

Instantiate, Prefabs, Classes 1 Answer

How do I access variables of prefabs I am loading via Resource.LoadAll, in script(C#) 2 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