Skip to content
Snippets Groups Projects
Commit 4f816c22 authored by Tobias Schöner's avatar Tobias Schöner
Browse files

fix: surplus favoriteDisplays caused by cloning will be deleted now

parent f6068f98
No related branches found
No related tags found
No related merge requests found
...@@ -13,7 +13,7 @@ public class FactFavorisation : MonoBehaviour, IPointerClickHandler ...@@ -13,7 +13,7 @@ public class FactFavorisation : MonoBehaviour, IPointerClickHandler
#endregion InspectorVariables #endregion InspectorVariables
#region Static Variables #region Static Variables
private static readonly UnityEvent<Fact, bool> ChangeFavoriteEvent = new(); public static readonly UnityEvent<Fact, bool> ChangeFavoriteEvent = new();
private static readonly List<Fact> favorites = new(); private static readonly List<Fact> favorites = new();
#endregion Static Variables #endregion Static Variables
...@@ -53,32 +53,34 @@ private void Start() ...@@ -53,32 +53,34 @@ private void Start()
fact = transform.GetComponent<FactWrapper>().fact; fact = transform.GetComponent<FactWrapper>().fact;
ChangeFavoriteEvent.AddListener(OnFavoriteChange); ChangeFavoriteEvent.AddListener(OnFavoriteChange);
// if there already was a favoriteDisplayPrefab child (e.g. due to cloning) remove it
gameObject.ForAllChildren(child => {
if (child.name.StartsWith(favoriteDisplayPrefab.name))
Destroy(child);
});
// instantiate new favoriteDisplay
favoriteDisplay = Instantiate(favoriteDisplayPrefab, transform);
// check if fact is currenty a favorite // check if fact is currenty a favorite
if (favorites.Contains(fact)) isFavorite = favorites.Contains(fact);
{
isFavorite = true; UpdateDisplay();
UpdateDisplay();
}
} }
#endregion UnityMethods #endregion UnityMethods
#region Implementation #region Implementation
private void OnFavoriteChange(Fact changedFact, bool isFavourite) private void OnFavoriteChange(Fact changedFact, bool isFavorite)
{ {
if (fact == changedFact) if (fact == changedFact)
{ {
this.isFavorite = isFavourite; this.isFavorite = isFavorite;
UpdateDisplay(); UpdateDisplay();
} }
} }
private void UpdateDisplay() private void UpdateDisplay()
{ {
if (!isFavorite) favoriteDisplay.SetActive(isFavorite);
Destroy(favoriteDisplay);
else
if (!favoriteDisplay)
favoriteDisplay = Instantiate(favoriteDisplayPrefab, transform);
} }
#endregion Implementation #endregion Implementation
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment