F
F
FireDrago192020-11-03 19:39:26
Unity
FireDrago19, 2020-11-03 19:39:26

Save Game in Unity. Does not return coordinates. How to set object coordinates?

Downloaded from Unity Assets SaveGame
It seems that everything saves on exit and restores on startup, but not the location of the objects (They appear in zero coordinates) (I did not save Vector3, but each separately because I can’t make new Vector3 later)

using UnityEngine;
using System.Collections;
using System.Linq;

public class SaveSystemSetup : MonoBehaviour {

  [SerializeField] private string fileName = "Profile.bin"; // file to save with the specified resolution
    [SerializeField] private bool dontDestroyOnLoad; // the object will move from one scene to another (you only need to add it once)

  public GameObject towerprefab;
  public int i, b;
  public string namee, level, xc, yc, zc;
  void Awake()
  {
    SaveSystem.Initialize(fileName);
    if (dontDestroyOnLoad) DontDestroyOnLoad(transform.gameObject);
    i = SaveSystem.GetInt("Numboftower");
    b = 0; 
    while (b < i)
    {
      namee = "coord" + b.ToString();
      level = "level" + b.ToString();
      xc = "x" + b.ToString();
      yc = "y" + b.ToString();
      zc = "z" + b.ToString();
      GameObject proj = Instantiate(towerprefab);
      proj.transform.position = new Vector3(SaveSystem.GetFloat(xc), SaveSystem.GetFloat(yc), SaveSystem.GetFloat(zc));
      proj.GetComponent<DiceLVL>().LVL = SaveSystem.GetInt(level);
      //proj.GetComponent<DiceLVL>().Dannie = 1;
      b++;
    }
  }
    // if the object is present in all game scenes, auto save before exiting
    // on some platforms there may not be an exit function, see the Unity help
    void OnApplicationQuit()
  {
    SaveSystem.SaveToDisk();
    SaveSystem.SetInt("Numboftower", GameObject.FindGameObjectsWithTag("Tower").Length);
    i = GameObject.FindGameObjectsWithTag("Tower").Length;
    b = 0;
    while (b < i)
    {
      namee = "coord" + b.ToString();
      level = "level" + b.ToString();
      xc = "x" + b.ToString();
      yc = "y" + b.ToString();
      zc = "z" + b.ToString();
      SaveSystem.SetFloat(xc, GameObject.FindGameObjectsWithTag("Tower")[b].transform.position.x);
      SaveSystem.SetFloat(yc, GameObject.FindGameObjectsWithTag("Tower")[b].transform.position.y);
      SaveSystem.SetFloat(zc, GameObject.FindGameObjectsWithTag("Tower")[b].transform.position.z);
      SaveSystem.SetInt(level, GameObject.FindGameObjectsWithTag("Tower")[b].GetComponent<DiceLVL>().LVL);
      b++;
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yuuki Wesp, 2020-11-20
@FireDrago19

Don't invent crutches. Especially with custom data save systems, they usually have a lot of problems on different platforms.
The easiest way to save data is:
PlayerPrefs
And don't use heavyweight operations in the OnApplicationQuit method, especially if it's synchronous.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question