Hello everyone.
I have the following code that is supposed to keep track of a group of elements, and whenever they all die, send a notification. It works by counting how many elements there are in the beginning, decreasing a counter every time one is destroyed, and sending a notification once the counter reaches 0.
It’s pretty straightforward and works fine when Notification is a class. However, when I change Notification to be a struct, it doesn’t.
If I look at output, with a class I get the following output:
(at start) Notification myNotification has 3 elements
(at update) Notification myNotification has 3 elements
if I change to a struct, I get the following:
(at start) Notification myNotification has 3 elements
(at update) Notification myNotification has 0 elements
How is that possible? That is before anything is even called, even if I remove the function DecreaseCount() the variable currentCount is set to 0 after being initialized to 3 at the very start. Is this some default construction of structs I’m not aware of?
Thank you for your help
public class NotificationManager : MonoBehaviour
{
public Notification[] notifications;
void Start()
{
foreach (Notification n in notifications) n.Setup();
}
void Update()
{
foreach (Notification n in notifications) n.Print();
}
[System.Serializable]
public /*struct*/ class Notification
{
public string name;
public Element[] elements;
private int currentCount;
public void Setup()
{
foreach (Element element in elements)
{
currentCount++;
element.OnDeath += DecreaseCount;
}
Print();
}
public void Print()
{
Debug.Log($"Notification {name} has {currentCount} elements");
}
public void DecreaseCount()
{
currentCount--;
if (currentCount == 0)
{
// notify
}
}
}
}