how to complete this piece of code.

Please tell me what needs to be fixed and how to complete this piece of code. This is what I managed to find and supplement, as well as correct, but I am not completely sure of the correct syntax and some more points: ObjectsPositions.Add (obj); // compiler swears obj cannot be converted from GameObject to SaveGameToM.myVector3


using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveGameToM: MonoBehaviour
{
public GameObject Cube1Pref;
public struct myVector3
{
public float x, y, z;
}
[SerializeField] public List ObjectsPositions = new List ();
public void Start ()
{
ObjectsPositions = Load ();
foreach (var pos in ObjectsPositions)
{
Instantiate (Cube1Pref, new Vector3 (pos.x, pos.y, pos.z), Quaternion.identity);
}
}
public void OnApplicationQuit ()
{
foreach (var obj in GameObject.FindGameObjectsWithTag (“Cube1”))
{
ObjectsPositions.Add (obj);
}
Save (ObjectsPositions);
} // Serialize this structure to JSON or BinarySerialize.
void List Load ()
{
return; // [deserialize from where we serialize in the Save method]
}
void Save (List positions)
{
// [serialize positions
}

People aren’t going to read your code if it isn’t readable. See this thread if you need help:

But on the ObjectsPositions.Add issue, you define it as a public List of type myVector3. I have no idea what a myVector3 is, other than assuming it has some relation to a Vector3 based entirely on its name.

In your foreach loop you call GameObject.FindGameObjectsWithTag (“Cube1”), which returns a collection of GameObjects. You then try to stuff those GameObjects one by one into ObjectsPositions. GameObjects aren’t whatever a myVector3 is, so the compiler spits in your face. Without knowing what a myVector3 is, I have no idea how to convert a GameObject to one, other than maybe you intended to use the GameObject’s postion. But that is just based on Vector3 being in the name of whatever a myVector3 type is.

2 Likes

-my Vector 3 This is the same vector 3, since simple vector 3 will not be serialized.
-ObjectsPositions is the name of the dynamic archive where I want to save the transform position of all objects with the tag “Cube1” for their further restoration

The essence of this saving is to find all objects by the tag “Cuba1” and save their coordinates to the List ObjectsPositions array. Then save the data, and later restore the objects based on the creation of new ones at the same coordinates from the array.

ObjectsPositions array. Then save the data, and later restore the objects based on the creation of new ones at the same coordinates from the array."

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
public class SaveGameToM: MonoBehaviour
{
public GameObject Cube1Pref;
public struct myVector3
{
public float x, y, z;
}
[SerializeField] public List <myVector3> ObjectsPositions = new List <myVector3> ();
public void Start ()
{
ObjectsPositions = Load ();
foreach (var pos in ObjectsPositions)
{
Instantiate (Cube1Pref, new Vector3 (pos.x, pos.y, pos.z), Quaternion.identity);
}
}
public void OnApplicationQuit ()
{
foreach (var obj in GameObject.FindGameObjectsWithTag ("Cube1"))
{
ObjectsPositions.Add (obj);
}
Save (ObjectsPositions);
} // Serialize this structure to JSON or BinarySerialize.
void List <myVector3> Load ()
{
return; // [deserialize from where we serialize in the Save method]
}
void Save (List <myVector3> positions)
{
// [serialize positions
}

This is pointless and may be prime source of an issue.
You should be using Vector3 instead creating another custom myVector3.
If vector is just a position, then you need use go transform position, to get it, or set it, while storing positions should be done in list, or array of Vector3 type.

2 Likes

Kill “myVector3” and use Unity’s Vector3 instead. It is a struct, not class and it already provides everything you need along with math functions and operator overloads.

https://docs.unity3d.com/ScriptReference/Vector3.html

Also take a look at JsonUtility.
https://docs.unity3d.com/ScriptReference/JsonUtility.html

Trying to implement your own vector class is a waste of time in this scenario. You literally gain nothing.

2 Likes
public void OnApplicationQuit ()
{
foreach (var obj in GameObject.FindGameObjectsWithTag ("Cube1"))
{
ObjectsPositions.Add (obj);
}
Save (ObjectsPositions);
}

You are trying to save a gameobject as another type. You want to save the objects position, thats why its failing. You even said in your original post that its complaining about it being a different type, so the answer is given to you already by the compiler.

You want ObjectPositions to be a list of type Vector3, and then save the gameobject.transform.position to that list. And it will work.

Thanks everyone for the advice