- Home /
event is called twice
I am trying to send an event that activates the Save method on another script. But for some reason, it seems like the event is called twice. (I currently call another method to test the code more easily)
public delegate void SaveDataTrigger();
public static event SaveDataTrigger SaveTheData;
public void CloseShop() {
SaveTheData();
SceneManager.LoadScene("MainMenu");
}
// other class
ShopManager.SaveTheData += Say;
public void Say() {
Debug.Log("hi");
}
this are the relevant parts of the code. The closeShop method is only called once, i tested it with Debug.Log(). But "hi" is printed twice. Maybe someone knows why this happens?
Edit: I tested again, and it turns out, that during one session, every time i click the button that call the CloseShop() method, the amount of times "hi" is Say() is executed, is raising by two.
One possible reason is the script is added to multiple (in this case two) GameObjects.
Answer by sacredgeometry · Mar 19 at 02:10 PM
It sounds like you are adding the event handler twice i.e. doing ShopManager.SaveTheData += Say;
twice.
You could log out the length of the invocation list and to see if its greater than 1.
You could log out the length of the invocation list and to see if its greater than 1.
How to do this?
Your answer
![](https://koobas.hobune.stream/wayback/20220613054134im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to select an int/float variable from another script in the inspector? 2 Answers
Argument 1: cannot convert from 'method group' to 'UnityAction' 1 Answer
Changing ScrollView content from the script 0 Answers
I'm unable to clear a variable that is used in multiple scripts. 0 Answers
How to get argument values from persistent listener of UnityEvent 2 Answers