Hi! trying to make a minimap without using a second camera. Does anybody know how to remove a null object from a dictionary? When player collects a star this star gets destroyed and I get an error “MissingReferenceException: The object of type ‘MiniMapWorldObject’ has been destroyed but you are still trying to access it.”
This is dictionary:
Dictionary<MiniMapWorldObject, MiniMapIcon> miniMapWorldObjectsLookup = new Dictionary<MiniMapWorldObject, MiniMapIcon>();
And this one loops through the dictionary:
void UpdateMiniMapicons()
{
//loop through the dictionary
foreach (var kvp in miniMapWorldObjectsLookup)
{
var miniMapWorldObject = kvp.Key;
var miniMapIcon = kvp.Value;
//get a valid map position for minimap icon
var mapPosition = WorldPositionTomapPosition(miniMapWorldObject.transform.position);
//apply new map position
miniMapIcon.RectTransform.anchoredPosition = mapPosition;
//minimap icons rotation to match the world objects rotation
var rotation = miniMapWorldObject.transform.rotation.eulerAngles;
//set minimap icons match
miniMapIcon.IconRectTransform.localRotation = Quaternion.AngleAxis(-rotation.y, Vector3.forward); //angle access is going to ask for an angle of how much the rotation is
}
}
Tried to use list below but still getting the same error “MissingReferenceException” and icon doesnt disappear from a minimap. Wondering if there is something wrong with this code below
List<MiniMapWorldObject> toBeRemoved = new List<MiniMapWorldObject>();
void Remove()
{
toBeRemoved.Clear();
foreach (var kv in miniMapWorldObjectsLookup)
{
if (kv.Key == null || kv.Value == null)
{
toBeRemoved.Add(kv.Key);
}
}
for (int i = 0; i < toBeRemoved.Count; i++)
{
miniMapWorldObjectsLookup.Remove(toBeRemoved[i]);
}
toBeRemoved.Clear();
}