C# 由于对象(System.Iinq)的当前状态,操作无效

C# 由于对象(System.Iinq)的当前状态,操作无效,c#,list,linq,unity3d,C#,List,Linq,Unity3d,所以我在Unity中制作一个基本的2D平台,我希望能够节省玩家完成每个关卡所需的时间,并在UI元素中显示最快的时间。我正在将时间写入一个文本文件(这很好),并将所有时间逐行读取到一个列表中,从那里我找到最低值等。但是,我的代码不起作用,当我从另一个脚本调用函数时,它会给我以下错误。我是C#的新手,所以非常感谢任何人能给我的帮助 谢谢 完整错误消息 using System.Collections; using System.Collections.Generic; using UnityEngi

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

谢谢

完整错误消息

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;
    }

}
InvalidOperationException:由于对象的当前状态,操作无效 System.Linq.Enumerable.Iterate[Single,Single](IEnumerable
1源代码,Single initValue,System.Func
3选择器) 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;
    }

}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
使用System.IO;
使用制度;
使用System.Linq;
公共类储蓄分数:单一行为{
void Start()
{
//ReadData();
}
公共静态void WriteData(浮点时间,字符串LevelLoaded)
{
尝试
{
//Debug.Log(“节省时间”);
StreamWriter sw=new StreamWriter(@“C:\Users\Theo\Documents\Unity Projects\V13\Platformer\Assets\Scripts\Highscores\Scores.txt”,true);
软件写入线(LevelLoaded+“”+时间);
sw.Close();
}
捕获(例外e)
{
Console.WriteLine(“异常:+e.Message”);
}
最后
{
Console.WriteLine(“执行最终块”);
}
}
公共静态void ReadData(字符串LevelLoaded)
{
List timesLevel1=新列表();
List timesLevel2=新列表();
List timesLevel3=新列表();
尝试
{
var lines=File.ReadAllLines(@“C:\Users\Theo\Documents\Unity Projects\V13\Platformer\Assets\Scripts\Highscores\Scores.txt”);
foreach(行中的var行)
{
如果(第行包含(“第1级”))
{
timesLevel1.Add(Convert.ToSingle(line));
}
else if(第行包含(“第2级”))
{
timesLevel2.Add(Convert.ToSingle(line));
}
否则,如果(第3行包含(“第3级”))
{
timesLevel3.Add(Convert.ToSingle(line));
}
}
}
捕获(例外e)
{
Console.WriteLine(“异常:+e.Message”);
}
最后
{
Console.WriteLine(“执行最终块”);
}
开关(水平负载)
{
案例“1级”:
UIManager.lowestTime=timesLevel1.Min();
打破
案例“2级”:
UIManager.lowestTime=timesLevel2.Min();
打破
案例“3级”:
UIManager.lowestTime=timesLevel3.Min();
打破
}
}

}

如果您查看
Enumerable.Min
()的文档,您将看到在“异常”下,它列出了
无效操作异常
,并给出原因:“源代码不包含任何元素”

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

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


因此,您正在读取的文件的格式可能与您预期的格式不符,从而导致列表为空,从而导致此错误。

谢谢,刚刚意识到这一点。忘记了我在阅读整行内容,我在每行添加了“LevelX”,以便为多个级别节省时间。所以我需要以某种方式排除这个前缀,只需将时间添加到列表中。