How to keep the rotation along with the position

The question is actually what: 1. I saved the triangle, for example, using Vector3. But the rotation of the triangle was not saved into the array; when all the triangles are restored, they will be restored to the prefab position. How to keep them rotate position? Vector 3 has only 3 variables. If they are in a separate array, then how will they choose the coordinates when restoring? 2. Really everyone does this and there is no way to simplify saving, well, it’s obscurantism to add code and search by tag for each object.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using UnityEngine.UI;



public class SaveGameToM : MonoBehaviour
{


    public GameObject Cube1Pref;
    public GameObject Triangle1Pref;



    [System.Serializable]
    public struct myVector3
    {
        public float x, y, z;
    }


    [System.Serializable]
    public struct myVector4
    {
        public float j, k, l;
    }




    [SerializeField] public List<myVector3> ObjectsPositions = new List<myVector3>();

    [SerializeField] public List<myVector4> ObjectsPositionsT = new List<myVector4>();

    public void Awake()
    {
        if (!File.Exists(Application.persistentDataPath + "saves"))

        {
            Directory.CreateDirectory(Application.persistentDataPath + "/saves");
        }
    }

    public void Start()
    {


        if (File.Exists(Application.persistentDataPath + "/saves/save.sav")& File.Exists(Application.persistentDataPath + "/saves/saveT.sav"))
        {
            BackMyCubes();
            BackMyTriangles();
        }

    }

    public void BackMyCubes()
    {
        ObjectsPositions = Load();

        foreach (var pos in ObjectsPositions)
        {
            Instantiate(Cube1Pref, new Vector3(pos.x, pos.y, pos.z), Quaternion.identity);
        }
        ObjectsPositions.Clear();
    }

    public void BackMyTriangles()
    {//n
        ObjectsPositionsT = LoadT();

        foreach (var posT in ObjectsPositionsT)//n
        {//n
            Instantiate(Triangle1Pref, new Vector3(posT.j, posT.k, posT.l), Quaternion.Euler(-90f,0.0f,0.0f));//n
        }//n
        ObjectsPositionsT.Clear();//n
    }//n



    public void ManualSaving()
    {
        if (File.Exists(Application.persistentDataPath + "/saves/save.sav"))
        {
            File.Delete(Application.persistentDataPath + "/saves/save.sav");
        }

        foreach (var obj in GameObject.FindGameObjectsWithTag("Cube1"))

        {
            myVector3 i = new myVector3();
            i.x = obj.transform.position.x;
            i.y = obj.transform.position.y;
            i.z = obj.transform.position.z;



            ObjectsPositions.Add(i);
        }

        Save(ObjectsPositions);
    }



    public void ManualSavingT()//n
    {//n
        if (File.Exists(Application.persistentDataPath + "/saves/saveT.sav"))//n
        {//n
            File.Delete(Application.persistentDataPath + "/saves/saveT.sav");//n
        }//n

        foreach (var objT in GameObject.FindGameObjectsWithTag("Triangle1"))//n

        {//n
            myVector4 t = new myVector4();//n
            t.j = objT.transform.position.x;//n
            t.k = objT.transform.position.y;//n
            t.l = objT.transform.position.z;//n
            ObjectsPositionsT.Add(t);//n
           


        }//n

        Save(ObjectsPositionsT);//n
    }//n






    public List<myVector3> Load()
    {
        List<myVector3> objects;
        BinaryFormatter formatter = new BinaryFormatter();

        using (FileStream stream = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Open))
        {
            objects = (List<myVector3>)formatter.Deserialize(stream);
        }


        return objects;

    }


    public List<myVector4> LoadT()//n
    {//n
        List<myVector4> objectsT;//n
        BinaryFormatter formatter = new BinaryFormatter();//n

        using (FileStream streamT = new FileStream(Application.persistentDataPath + "/saves/saveT.sav", FileMode.Open))//n
        {//n
            objectsT = (List<myVector4>)formatter.Deserialize(streamT);//n
        }//n


        return objectsT;//n

    }//n
  



    void Save(List<myVector3> positions)
    {


        BinaryFormatter formatter = new BinaryFormatter();
        using (FileStream stream = new FileStream(Application.persistentDataPath + "/saves/save.sav", FileMode.Create))
        {
            formatter.Serialize(stream, positions);
            stream.Close();
        }
        positions.Clear();

    }



    void Save(List<myVector4> positionsT)//n
    {//n


        BinaryFormatter formatter = new BinaryFormatter();//n
        using (FileStream streamT = new FileStream(Application.persistentDataPath + "/saves/saveT.sav", FileMode.Create))//n
        {
            formatter.Serialize(streamT, positionsT);//n
            streamT.Close();//n
        }
        positionsT.Clear();//n

    }//

}

You’re already using structs that you’ve defined yourself, why not add any variables you need?

[System.Serializable]
    public struct myVector3
    {
        public float x, y, z;
        public float eulerX, eulerY, eulerZ;
    }

Then you’ll have a list of all of the data for that object. If you need to save something else about the object, like its size or health or something, you can just add that in as well. (Of course the name “myVector3” isn’t really accurate anymore, but that’s easily adjusted)

You can even put an “objectType” or “prefabName” variable into the class, and save objects of other types as well.

There are tons of game-saving packages on the asset store, some of them are free. I’m not sure I follow that last bit though, what does anything you’re doing here have to do with searching by tag?

During saving, all triangle objects that are built on the map during the game are found by tag, after which the coordinates of each are saved into an array of vector 3 and serialized for saving. Then, in the reverse order, they are restored at the start of the game. This is why I don’t know how to keep the rotation position. After all, after restoration, the array is cleared for subsequent saving. But here’s what I don’t understand: public swimming eulerX, eulerY, euler; will be saved to an array as a parameter of each individual triangle and, after deserialization and loading the game, will they restore the rotation position for each object?

During saving, all triangle objects that are built on the map during the game are found by tag, after which the coordinates of each are saved into an array of vector 3 and serialized for saving. Then, in the reverse order, they are restored at the start of the game. This is why I don’t know how to keep the rotation position. After all, after restoration, the array is cleared for subsequent saving. But here’s what I don’t understand: public swimming eulerX, eulerY, euler; will be saved to an array as a parameter of each individual triangle and, after deserialization and loading the game, will they restore the rotation position for each object?

Well you have to apply them to the rotation in your code (like on line 66, replace Quaternion.identity with Quaternion.Euler(eulerX, eulerY, eulerZ), but yes.