How can I separate serializable class into its own file?
As the question states, how can I separate my GameData class which is serializable, into its own file?
I tried it and set GameData to be public class, but when I try to instantiate object of it into other class function Unity gives error:
error CS0246: The type or namespace name `GameData' could not be found. Are you missing a using directive or an assembly reference?
[Serializable]
public class GameData
{
public float experience = Helper.DEFAULT_EXPERIENCE;
public float score = Helper.DEFAULT_SCORE;
public float winPercent = Helper.DEFAULT_WIN_PERCENT;
public int tasksSolved = Helper.DEFAULT_NUM_OF_TASKS_SOLVED;
public int correct = Helper.DEFAULT_NUM_OF_CORRECT;
public int difficulty = Helper.DEFAULT_DIFFICULTY;
public bool gameStateDirty = Helper.DEFAULT_GAME_STATE_DIRTY;
public bool gameIsNormal = Helper.DEFAULT_GAME_IS_NORMAL;
}
Yes I am noob C# programmer but would like little help on the topic. Thanks.
Post your script of GameData. Remember that the file name has to be the same as the class name.
Yes my file was named GameData and yes if I comment out GameData which is in another class file and try to use GameData from a separate file, unity complains with the above error. As if [Serializable] annotation make the class internal or invisible? Not sure.
Answer by Fydar · Feb 25, 2016 at 10:23 PM
It sounds like to me you want to read up about "SerializableObject". Here is an example of using it in a class:
[CreateAssetMenu(fileName = "Item", menuName = "Inventory/Item", order = 1)]
public class Item : ScriptableObject {
public string ItemName = "Sword";
public int ID = 0;
}
I don't know exactly why that error was produced because I don't know what us causing it. I see your "GameData" class, but seeing the code which is attempting to access it would be helpful.
Your answer
Follow this Question
Related Questions
Deserialize an outside object in Unity 0 Answers
Null Exception while serialize json string into txt file 0 Answers
Start function dont work 1 Answer
Cannot read past end of stream, MemoryStream 1 Answer
c# - Array Index is out of range 1 Answer