0
votes

I am trying to restart my current level from a script attached to the Main Camera through:

Application.LoadLevel (Application.loadedLevel);

Once I complete it, multiple instances of one script are running (There may be many I haven't checked yet). For example, I use one script that subscribes to events from multiple scripts to decrease the "bullets left" and "score".

When I noticed that they get decreased or increased by multiple numbers instead of having one step at a time, I printed using Debug.Log() for when I shoot. It prints twice after the first Application.loadLevel, then thrice after the 2nd reset and so on. Every time I restart the scene , the script is running that many times with that many decrease or increase in "Score" / "bullets left".

There's one more script that has a null animator issue which I believe is due to the multiple instances issue as well.

Everything looks like it's getting messed up due to multiple instances issue. ALSO I HAVE ABSOLUTELY NO DontDestroyOnLoad anywhere in my game.

I use events and delegates. In the place from where I publish the event, when I use Debug.Log() to see if it publishes so many times, it only prints it once as expected. But the scripts which listens to these events :

void OnEnable()
{
    BulletController.scoreUP += scoreIncrease;
    BulletController.bulletCreated += bulletDecrease;
}
void OnDisable()
{
    BulletController.scoreUP -= scoreIncrease;
    BulletController.bulletCreated -= bulletDecrease;
}

listens and executes twice, or thrice or so on for every restart of scene.

1
Just to be sure, you are certain that the OnDisable call happens?Bart

1 Answers

1
votes

You're issue is to do with your OnDisable function. When destroying objects, OnDisable doesn't get called.

You need a function called OnDestroy which unsubscribes from those events in the same way (You probably want to keep your OnDisable function in case there are any objects you do disable).