Answer the question
In order to leave comments, you need to log in
Why does the Unity console display multiple object status messages when serializing objects?
I read an article about object serialization in Unity.
Based on it, I wrote such a small prototype:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;
[System.Serializable]
public class LevelInfo
{
public int num;
}
public class Ser : MonoBehaviour
{
public int numLevelWrite;
public int numLevelRead;
private LevelInfo LevelWrite;
private LevelInfo LevelRead;
private void Start()
{
LevelWrite = new LevelInfo();
LevelRead = new LevelInfo();
LevelWrite.num = numLevelWrite;
LevelRead.num = numLevelRead;
}
private void Update()
{
if (Input.GetKeyDown(KeyCode.W))
{
Debug.Log("Before Write:\n" + "LevelWrite.num = " + LevelWrite.num + "\n LevelRead.num = " + LevelRead.num);
Write(LevelWrite);
Debug.Log("After Write/Before Read:\n" + "LevelWrite.num = " + LevelWrite.num + "\n LevelRead.num = " + LevelRead.num);
}
if (Input.GetKeyDown(KeyCode.R))
{
Debug.Log("After Write/Before Read:\n" + "LevelWrite.num = " + LevelWrite.num + "\n LevelRead.num = " + LevelRead.num);
LevelRead = Read(LevelRead);
Debug.Log("After Read:\n" + "LevelWrite.num = " + LevelWrite.num + "\n LevelRead.num = " + LevelRead.num);
}
}
private void Write(LevelInfo lev)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream StreanIn = new FileStream(@"C:\Users\LionsGate\Desktop\t.dat", FileMode.Create);
formatter.Serialize(StreanIn, lev);
StreanIn.Close();
}
private LevelInfo Read(LevelInfo lev)
{
BinaryFormatter formatter = new BinaryFormatter();
FileStream StreanOut = new FileStream(@"C:\Users\LionsGate\Desktop\t.dat", FileMode.Open);
lev = (LevelInfo)formatter.Deserialize(StreanOut);
StreanOut.Close();
return lev;
}
}
Answer the question
In order to leave comments, you need to log in
I officially declare that I'm stupid. The script was attached to two objects. Here is the message displayed twice...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question