sortedlist< key,value>:错误在更改循环内的值时,循环浏览列表 / unity,c#

发布于 2025-01-19 20:28:36 字数 1572 浏览 1 评论 0原文

我在为我在 Unity 中制作的游戏中编写此功能时遇到问题。我使用 SortedList存储我的玩家输入触发器的可交互对象以及它们与他的距离。我希望他只能与一个物体交互,即离他最近的物体。因此,我遍历列表并使用每帧的新距离更新键的浮点值,然后将对象的 canInteract bool 设置为 true(如果它是列表索引 0 中的对象,这意味着最接近玩家) 。

这可行,但 Unity 向我抛出一个错误,指出我正在更改值,而它仍在迭代 KeyValue 对,如下面的屏幕截图所示。

输入图片这里的描述

我的代码如下:

public SortedList<GameObject, float> interactablesInRange = new SortedList<GameObject, float>();

public void CheckForInteractableDistance()
    {
        foreach (KeyValuePair<GameObject, float> interactable in interactablesInRange)
        {
            interactablesInRange[interactable.Key] = DistanceChecker(gameObject, interactable.Key);
            Debug.Log(interactable.Key + " distance from player: " + interactable.Value);

            if (interactablesInRange.IndexOfValue(interactable.Value) == 0)
            {
                interactable.Key.GetComponent<WeaponPickup>().canInteract = true;
            }
            else
            {
                interactable.Key.GetComponent<WeaponPickup>().canInteract = false;
            }
        }
    }


public float DistanceChecker(GameObject fromObj, GameObject toObj)
    {
        float distance = Vector3.Distance(fromObj.transform.position, toObj.transform.position);

        return distance;
    }

我试图找到一种方法来避免该错误,同时仍然做同样的事情。有人建议我也许可以使用 LINQ 来做到这一点,但我不知道如何使用 LINQ 或者这是否可以解决问题,因为我仍然必须在迭代列表时更改值

I am having trouble programming this feature for a game i am making in Unity. I use a SortedList<GameObject, float> to store the interactable objects my player enter trigger with, and their distance from him. I want him to be able to interact only with one object, the one closest to him. For that reason i run through the list and update the float value of the keys with the new distance each frame, and then set the object's canInteract bool to true, if its the one in index 0 of the list, meaning the closest to the player.

This works, but Unity throws me an error saying that i am altering the values while it is still iterating through the KeyValue pairs, as shown in the screenshot below.

enter image description here

My code is the following:

public SortedList<GameObject, float> interactablesInRange = new SortedList<GameObject, float>();

public void CheckForInteractableDistance()
    {
        foreach (KeyValuePair<GameObject, float> interactable in interactablesInRange)
        {
            interactablesInRange[interactable.Key] = DistanceChecker(gameObject, interactable.Key);
            Debug.Log(interactable.Key + " distance from player: " + interactable.Value);

            if (interactablesInRange.IndexOfValue(interactable.Value) == 0)
            {
                interactable.Key.GetComponent<WeaponPickup>().canInteract = true;
            }
            else
            {
                interactable.Key.GetComponent<WeaponPickup>().canInteract = false;
            }
        }
    }


public float DistanceChecker(GameObject fromObj, GameObject toObj)
    {
        float distance = Vector3.Distance(fromObj.transform.position, toObj.transform.position);

        return distance;
    }

I am trying to find a way to avoid that error, while still doing the same thing. I was suggested i can maybe do that with LINQ, but i dont know how to use LINQ or if this would fix the issue since i still have to change the value while iterating through the list

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

忘年祭陌 2025-01-26 20:28:36

在审查了评论后,我意识到在这种情况下使用分类列表并不是很不可思议的,并且我以前使用它的方式是不正确的。

在网上进行了更多的研究之后,我在此线程中发现了评论( https://answers.unity.com/questions/1408498/sorting-a-list--list--list--list-as-list-list-a--a-variable.html )基本上可以回答我的问题,并做我的问题想要在一行代码中使用简单的LINQ表达式执行!

因此解决了案例!如果您想做类似的事情,我会在下面发布代码:


private void SortByDistance()
    {
        foreach (GameObject interactable in interactablesInRange)
        {
            interactable.GetComponent<WeaponPickup>().distanceFromPlayer = DistanceChecker(gameObject, interactable);
        }

        interactablesInRange = interactablesInRange.OrderBy(e => e.GetComponent<WeaponPickup>().distanceFromPlayer).ToList();

    }

之后,我只需检查每个互动脚本,如果可相互作用是列表的索引[0]中的gameObject,如果是的,则我将CanInteract Bool设置为true。

Well after reviewing the comments i realised that it was not nessecary to use a SortedList in that case and that the way i used it before was incorrect.

After a bit more research online i found out a comment in this thread (https://answers.unity.com/questions/1408498/sorting-a-list-based-of-a-variable.html) that basically answers my question and does what i want to do using a simple LINQ expression in one line of code!

So case is solved! In case you want to do something similar i post the code below:


private void SortByDistance()
    {
        foreach (GameObject interactable in interactablesInRange)
        {
            interactable.GetComponent<WeaponPickup>().distanceFromPlayer = DistanceChecker(gameObject, interactable);
        }

        interactablesInRange = interactablesInRange.OrderBy(e => e.GetComponent<WeaponPickup>().distanceFromPlayer).ToList();

    }

After that i just check in each interactables script if the interactable is the GameObject in index [0] of the list, and if it is i set canInteract bool to true.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文