Home > database >  SortedList<Key, Value>: Error while changing the values inside a loop, while looping through t
SortedList<Key, Value>: Error while changing the values inside a loop, while looping through t

Time:04-23

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

CodePudding user response:

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.

  • Related