- Home /
The question is answered, right answer was accepted
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?,
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