Problem with Dictionary.Remove
Hi all!
I have a Dictionary parameter in my script with Vector3 type of keys and GameObject values.
Vector3s are used to keep track of the positions of a corresponding GameObject in a scene.
The problem comes when I try to remove the gameobject (and the key). Sometimes the Remove() method doesn't find the corresponding key even though the value of the key in Dictionary and of the corresponding parameter is the same.
I try to remove the key-value pair like this:
public void markDestroyedBallPosition (Vector3 posDestroy) if (ballsInScene.Remove(posDestroy)) { removedBallPositions.Add(reducedBalls, posDestroy); %|-172957788_3|% } else { }
I have debugged this and the value of the posDestroy parameter corresponds to the Key in the ballsInScene Dictionary.
If someone from the community could help, I would appreciate it a lot. I am totally stuck with this bug.
Thanks in advance! - Jukka
There is something wrong with the code-section. For some reason the system puts some weird character-lines in the code-section.
Answer by SohailBukhari · Mar 30, 2017 at 01:30 PM
Are you trying to remove a single value or all matching values? The reason you don't get a key back when querying on values is because the dictionary could contain multiple keys paired with the specified value. I wrote this script and i tested value removes using specified key, use RemoveByValue() to remove value against your key.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
private Dictionary<Vector3, GameObject> _dic = new Dictionary<Vector3, GameObject>();
public GameObject value;
// Use this for initialization
void Start () {
_dic.Add(Vector3.back, value);
GameObject someValue = value;
RemoveByValue<Vector3,GameObject>(_dic, someValue);
}
private void RemoveByValue<TKey, TValue>(Dictionary<TKey, TValue> _dic, TValue someValue)
{
List<TKey> itemsToRemove = (from pair in _dic where pair.Value.Equals(someValue) select pair.Key).ToList();
foreach (TKey item in itemsToRemove)
{
Debug.Log(item.ToString());
_dic.Remove(item);
}
}
}
Thanks for the input, but there isn't duplicate $$anonymous$$ey entries in the Dictionary.
Somewhere I remembered that comparing Vector3s (which the Remove() does in my case) isn't very good thing. I searched how other people have circulated this and I found a post about the problem (this).
However, in my case, comparing two Vector3s in a separate function is impossible because I would need first to get the $$anonymous$$ey based on the value of the other Vector3.
Only other option which I can think of would be using the Value and delete the entry based on that. The problem with this method is that all the GameObjects are with the same name in the scene so I don't know would Unity recognize different gameobjects from each other...
I'm too noob to understand how does your method work....:D
'private void RemoveByValue(Dictionary _dic, TValue someValue)'
This has too many variable with the same name so that I would understand what are passed as parameters to the method and which the method creates itself.
Please help!
Your answer

Follow this Question
Related Questions
Trying to get gameobject from one list to follow gameobjects in another list 1 Answer
Problem with reading values from scripts on GameObjects in Lists 1 Answer
Trying to program two buttons to appear when the player in my game dies 0 Answers
List resulting in out of range 0 Answers
Finding an object's list index via the object's attached script 1 Answer