【问题标题】:Operation is not valid due to the current state of the object (System.Iinq)由于对象的当前状态,操作无效 (System.Iinq)
【发布时间】:2017-07-04 17:03:01
【问题描述】:

所以我正在 Unity 中制作一个基本的 2D 平台游戏,我希望能够节省玩家完成每个关卡所需的时间,并在 UI 元素中显示最快的时间。我正在将时间写入文本文件(这工作正常),并将所有时间逐行读取到列表中,从那里我找到最低值等。但是,我的代码不起作用,它给了我以下当我从另一个脚本调用函数时出错。我是 C# 新手,因此非常感谢任何人能给我的帮助!

谢谢!

完整的错误信息

InvalidOperationException:由于对象的当前状态,操作无效 System.Linq.Enumerable.Iterate[Single,Single](IEnumerable1 source, Single initValue, System.Func3 选择器) System.Linq.Enumerable.Min(IEnumerable`1 源) SaveScores.ReadData (System.String LevelLoaded) (在 Assets/Scripts/Highscores/SaveScores.cs:73) GameManager.Update () (在 Assets/Scripts/GameManager.cs:45)

代码

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using System;
using System.Linq;

public class SaveScores : MonoBehaviour {

void Start()
{
    //ReadData();
}

public static void WriteData(float time, string LevelLoaded)
{
    try
    {
        //Debug.Log("Saving time");
        StreamWriter sw = new StreamWriter(@"C:\Users\Theo\Documents\Unity Projects\V13\Platformer\Assets\Scripts\Highscores\Scores.txt", true);
        sw.WriteLine(LevelLoaded + " " + time);

        sw.Close();
    }
    catch(Exception e)
    {
        Console.WriteLine("Exception: " + e.Message);
    }
    finally
    {
        Console.WriteLine("Executing final block");
    }
}

public static void ReadData(string LevelLoaded)
{
    List <float> timesLevel1 = new List<float>();
    List <float> timesLevel2 = new List<float>();
    List <float> timesLevel3 = new List<float>();

    try
    {
        var lines = File.ReadAllLines(@"C:\Users\Theo\Documents\Unity Projects\V13\Platformer\Assets\Scripts\Highscores\Scores.txt");

        foreach (var line in lines)
        {
            if (line.Contains("Level1"))
            {
                timesLevel1.Add(Convert.ToSingle(line));
            }
            else if (line.Contains("Level2"))
            {
                timesLevel2.Add(Convert.ToSingle(line));
            }
            else if (line.Contains("Level3"))
            {
                timesLevel3.Add(Convert.ToSingle(line));
            }
        }
    }
    catch (Exception e)
    {
        Console.WriteLine("Exception: " + e.Message);
    }
    finally
    {
        Console.WriteLine("Executing final block");
    }

    switch (LevelLoaded)
    {
        case "Level1":
            UIManager.lowestTime = timesLevel1.Min();
            break;
        case "Level2":
            UIManager.lowestTime = timesLevel2.Min();
            break;
        case "Level3":
           UIManager.lowestTime = timesLevel3.Min();
            break;
    }

}

}

【问题讨论】:

    标签: c# list linq unity3d


    【解决方案1】:

    如果您查看Enumerable.Min&lt;float&gt; (https://msdn.microsoft.com/en-us/library/bb361144(v=vs.110).aspx) 的文档,您会看到在“例外”下它列出了InvalidOperationException,并给出了原因:“源不包含任何元素”。

    这可能意味着您正在查看的列表不包含任何元素。

    在查看该列表中的内容时,您似乎对您期望的内容有些困惑。您对line.Contains("Level1") 进行了测试,但如果测试成功,则调用Convert.ToSingle(line),如果该行中有任何字符串数据(例如,如果line 确实包含该字符串,则转换将失败)。

    因此,您正在阅读的文件格式似乎不是您期望的格式,导致您的列表为空,从而导致此错误。

    【讨论】:

    • 谢谢,刚刚意识到。忘了我正在阅读整行,我在每一行都添加了“LevelX”,以便我节省多个级别的时间。所以我需要以某种方式排除这个前缀,只需将时间添加到列表中。
    猜你喜欢
    • 2010-10-18
    • 1970-01-01
    • 2020-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多