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
#endregion InspectorVariables
#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();
#endregion Static Variables
......@@ -53,32 +53,34 @@ private void Start()
fact = transform.GetComponent<FactWrapper>().fact;
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
if (favorites.Contains(fact))
{
isFavorite = true;
UpdateDisplay();
}
isFavorite = favorites.Contains(fact);
UpdateDisplay();
}
#endregion UnityMethods
#region Implementation
private void OnFavoriteChange(Fact changedFact, bool isFavourite)
private void OnFavoriteChange(Fact changedFact, bool isFavorite)
{
if (fact == changedFact)
{
this.isFavorite = isFavourite;
this.isFavorite = isFavorite;
UpdateDisplay();
}
}
private void UpdateDisplay()
{
if (!isFavorite)
Destroy(favoriteDisplay);
else
if (!favoriteDisplay)
favoriteDisplay = Instantiate(favoriteDisplayPrefab, transform);
favoriteDisplay.SetActive(isFavorite);
}
#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