microsoft / MapsSDK-Unity

This repository contains samples, documentation, and supporting scripts for Maps SDK, a Microsoft Garage project.
MIT License
648 stars 127 forks source link

Can't destroy MapPin prefabs #177

Closed cafigz closed 2 years ago

cafigz commented 2 years ago

Hi!

I'm instantiating Map Pins from a database. I want to be able to add/remove prefabs during runtime, but whenever I try to destroy the Prefabs I get these errors:

MissingReferenceException: The object of type 'MapPin' has been destroyed but you are still trying to access it.
Your script should either check if it is null or you should not destroy the object.
Microsoft.Maps.Unity.MapPin.SynchronizeLayers (System.Collections.Generic.IReadOnlyList`1[T] mapPins, Microsoft.Maps.Unity.MapRenderer mapRenderer) (at Library/PackageCache/com.microsoft.maps.unity@0.11.1/Runtime/Scripts/MapRenderer/MapPin/MapPin.cs:200)
Microsoft.Maps.Unity.MapPinLayer.UpdateMapPinsInView (System.Object sender, System.EventArgs args) (at Library/PackageCache/com.microsoft.maps.unity@0.11.1/Runtime/Scripts/MapRenderer/MapPin/MapPinLayer.cs:223)
Microsoft.Maps.Unity.MapRenderer.Update () (at Library/PackageCache/com.microsoft.maps.unity@0.11.1/Runtime/Scripts/MapRenderer/MapRenderer.cs:295)
Your script should either check if it is null or you should not destroy the object.
Microsoft.Maps.Unity.MapPinLayer.OnDisable () (at Library/PackageCache/com.microsoft.maps.unity@0.11.1/Runtime/Scripts/MapRenderer/MapPin/MapPinLayer.cs:162)```

Basically what I'm doing is:
When I instantiate the map pin I add a tag to the prefab:

var mapPin = Instantiate(_mapPinPrefab); mapPin.tag = "MapPinPrefab"; mapPinsCreatedThisFrame.Add(mapPin);


When I want to destroy I do:

public void DestroyAllPrefabs() { int i = 0; foreach (GameObject gameObject in GameObject.FindGameObjectsWithTag("MapPinPrefab")) { Destroy(gameObject); } }



I need to clear the list of map pins in MapPin.cs, but it's read only: 
public static void SynchronizeLayers(IReadOnlyList<MapPin> mapPins, MapRenderer mapRenderer)

Basically something like: MapPin.mapPins.Clear();

Best regards!
kircher1 commented 2 years ago

Are you adding the MapPins to a MapPinLayer? If so, before destroying the pins, try removing them from the MapPinLayer's MapPins collection.

cafigz commented 2 years ago

Hi! How should I do that? What's the method to remove a mapPin from the MapPins list? Best regards

cafigz commented 2 years ago

Got it to work, thanks! All I had to do was: _mapPinLayer.MapPins.Clear();

cafigz commented 1 year ago

Hi! Thanks for answering. How should I do that? What is the method to remove the MapPin from them MapPins list? Thanks in advance!

Obter o Outlook para Androidhttps://aka.ms/AAb9ysg


From: kircher1 @.> Sent: Monday, June 27, 2022 6:42:39 PM To: microsoft/MapsSDK-Unity @.> Cc: cafigz @.>; State change @.> Subject: Re: [microsoft/MapsSDK-Unity] Can't destroy MapPin prefabs (Issue #177)

Are you adding the MapPins to a MapPinLayer? If so, before destroying the pins, try removing them from the MapPinLayer's MapPins collection.

— Reply to this email directly, view it on GitHubhttps://github.com/microsoft/MapsSDK-Unity/issues/177#issuecomment-1167667314, or unsubscribehttps://github.com/notifications/unsubscribe-auth/AYWGARYMHSWSUZSUPS7MMPDVRHRY7ANCNFSM5Z4ZJLHQ. You are receiving this because you modified the open/close state.Message ID: @.***>