S
S
SkyCrusher2020-02-21 16:28:14
C++ / C#
SkyCrusher, 2020-02-21 16:28:14

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;
    }
}


But when I run PlayMode, I get this feed of messages in the console.
5e4fda4964f64945631899.png
Then I close, restart Unity, change the values ​​of open variables and get a different feed.
5e4fda5350fd7409584076.png
I looked in the debugger. the values ​​change. But it is not clear why Debug.Log() is called twice every time and what happens in general? Unity creates its own instance of a class? But after all, private class variables do not directly contact the inspector ... In general, help me figure it out.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SkyCrusher, 2020-03-02
@SkyCrusher

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 question

Ask a Question

731 491 924 answers to any question