- Home /
What is the infinite loop in the following code?
This is pretty bizarre, and to be frank I'm looking for a lark fix where somewhere my code is the issue. But I haven't found any.
I'm just a newbie developer that was messing around with a game making course. Which would probably mean that it's a mistake in the code somewhere. But I've taken it apart, and it's still not something that is resolvable with what I know. It might just mean there's a complication that others know about, but I sure don't know about it.
protected override void OnCollide(Collider2D coll)
{
if (coll.name == "MC")
{
Debug.Log("collecting");
GameManager.instance.SaveState();
string sceneName = sceneNames[Random.Range(0, sceneNames.Length)];
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
Where OnCollide is just something that is called in "collidable"'s Update() for each BoxCollider2DComponent.OverlapCollider hit, passing the hit. Pretty standard stuff.
This game making course is not very complicated. As soon as it loads scene, everything should be destroyed, and assuming that 0,0 is not on top of the object calling OnCollide, nothing should result in an infinite loop. Obviously, that's the problem. Unity's editor player freezes and the scene cycles to itself, with infinite loops of SampleScene (not loaded), SampleScene and SampleScene, SampleScene (not loaded). As we all know, and I know now, loading the same scene is not the problem.
MC is not destroyed on load. I thought at first perhaps, that when it was loading, MC was preserved in that position and that the loadscenecollidable, for lack of a better term, was triggered. There was a fix to that that didn't seem to resolve that. When loadscenecollidable was set to not collide after the first collision, nothing changed. So OnCollide was not retriggered before loading. Maybe load scene reloaded the whole thing and OnCollide was triggered in the next scene, while MC was still there.
Looking for answers, it seemed to be that whenever there was an infinite loop, anywhere, that that was a likely candidate for this to happen. So a long trip back in my code to look for any loops to other loops, anywhere. Naturally there were no loops like that in so simple a project. So that's not something that is a problem. Although what I found next isolates the problem to this code in particular.
I wondered whether the code I SceneManager.sceneLoaded += was an infinite loop. I took it away. Infinite loop. I put it back. Infinite loop. It only causes one additional call a scene, but obviously I already had a SceneManager.sceneLoaded -=. Playing around with variations of that didn't help. I put in some Debug.Logs. What was in sceneLoaded was not even called. So then I moved the MC to a different position before LoadScene was called, which was what was in the sceneLoaded code, but that didn't break the loop, again. No real code in anything else sceneLoaded called and kind of disproves my first and second theories.
I took away LoadScene. No problems then. I took away the OnCollide, so it defaulted to something it derived from that stopped after triggering the first time. Did in fact stop after triggering the first time. I rewrote the code. I copied the code in from code that worked in case the ASCII was cursed. Everything worked as long as LoadScene was not in there.
Finally, I destroyed the object after LoadScene. I destroyed the object after moving MC. Guess what happened. MC was stuck in place, elsewhere, with the same infinite loop. I waited out in case all these triggers were from before all this happened. Still infinite.
One more thing. When MC is not DoNotDestroyOnLoad, and Awake does not destroy it if its a copy, it also works without an infinite loop, but no other object really works after a LoadScene either. Full of "missing scripts", but I'll get to that problem when I come to it. I've heard DoNotDestroyOnLoad doesn't remove scripts on load, but this post is long as is without reference to that. https://forum.unity.com/threads/scenemanager-loadscene-does-not-destroy-correctly-the-current-scene.378376/#post-2786778
Sure as hell SampleScene is in the build, else it won't load in the first place.
Your guess is better than mine.