Answer the question
In order to leave comments, you need to log in
Is the input string malformed?
This error pops up when you press the F button, which should unload the save from the xml file into the game
FormatException: Input string was not in a correct format.
System.Number.ParseSingle (System.String value, System.Globalization.NumberStyles options, System.Globalization.NumberFormatInfo numfmt) (at :0)
System.Single.Parse (System.String s, System.Globalization.NumberStyles style, System. Globalization.NumberFormatInfo info) (at :0)
System.Single.Parse (System.String s) (at :0)
GameHelper.GenerateScene (System.Xml.Linq.XElement root) (at Assets/GameHelper.cs:56)
GameHelper .Load() (at Assets/GameHelper.cs:50)
GameHelper.Update() (at Assets/GameHelper.cs:18)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Xml.Linq;
using System.IO;
public class GameHelper : MonoBehaviour
{
private string path;
public List<SaveableObject> objects = new List<SaveableObject>();
private void Awake()
{
path = Application.persistentDataPath + "/textsave.xml";
}
private void Update()
{
if (Input.GetButtonDown("Jump")) { Save(); }
if (Input.GetKeyDown(KeyCode.F)) { Load(); }
}
public void Save() {
XElement root = new XElement("root");
foreach(SaveableObject obj in objects)
{
root.Add(obj.GetElement());
}
Debug.Log(root);
root.AddFirst(new XElement("score", Data.score));
XDocument saveDoc = new XDocument(root);
File.WriteAllText(path, saveDoc.ToString());
Debug.Log(path);
}
public void Load() {
Debug.Log("LOADING IS COMPLETE");
XElement root = null;
if (!File.Exists(path))
{
if (File.Exists(Application.persistentDataPath + "/settings.xml"))
root = XDocument.Parse(File.ReadAllText(Application.persistentDataPath + "/settings.xml")).Element("root");
}
else {
root = XDocument.Parse(File.ReadAllText(path)).Element("root");
}
if (root == null) {
Debug.Log("Level load failed");
return;
}
GenerateScene(root);
}
private void GenerateScene(XElement root) {
foreach (XElement instance in root.Elements("instance"))
{
Vector3 position = Vector3.zero;
position.x = float.Parse(instance.Attribute("x").Value);
position.y = float.Parse(instance.Attribute("y").Value);
position.z = float.Parse(instance.Attribute("z").Value);
Instantiate(Resources.Load<GameObject>(instance.Value), position, Quaternion.identity);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question