BeautifulGollum
@BeautifulGollum
C# до потери пульса

Входная строка имеет неверный формат?

Вылезает такая ошибка при нажатии на кнопку F, которая должна выгрузить сохранение из xml файла в игру

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);
        }
    }
}
  • Вопрос задан
  • 171 просмотр
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы