sortedlist< key,value>:错误在更改循环内的值时,循环浏览列表 / unity,c#
我在为我在 Unity 中制作的游戏中编写此功能时遇到问题。我使用 SortedList
这可行,但 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.
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在审查了评论后,我意识到在这种情况下使用分类列表并不是很不可思议的,并且我以前使用它的方式是不正确的。
在网上进行了更多的研究之后,我在此线程中发现了评论( https://answers.unity.com/questions/1408498/sorting-a-list--list--list--list-as-list-list-a--a-variable.html )基本上可以回答我的问题,并做我的问题想要在一行代码中使用简单的LINQ表达式执行!
因此解决了案例!如果您想做类似的事情,我会在下面发布代码:
之后,我只需检查每个互动脚本,如果可相互作用是列表的索引[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:
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.