Unity 2D Course - Lesson 98

Hello. I’m trying to make my ScenePersist which contains my coins and enemies for a level act properly upon death and running into issues.

My game has 2 levels, and whenever I die on level 1, my Unity window crashes and I need to force close it. Even weirder, it seems to work about half the time when I’m on level 1 and die. Sometimes it will restart the game and other times it will crash.

Aside from this, ScenePersist is working completely fine. When I start a new level or lose a life, it’s acting like I want.

Within ScenePersist, I have a public function that only contains Destroy(gameObject). This is called twice: once within my GameSession when dying and once within LevelExit when I touch a door. Based on commenting and print statements, I’m positive that this Destroy(gameObject) line is giving me issues, though I can’t understand its correlation to Unity freezing.

1 Like

As a follow up to this, I copy/pasted the exact course resource code for GameSession, LevelExit, and ScenePersist from the gitlab link. I am still having the same problem.

1 Like

Hi adj0131,

Welcome to our community. :slight_smile:

Another student reported a similar problem. Did you happen to post it on Udemy as well?

I’m sharing a potential solution here in case you haven’t found any yet. First of all, check if your LoadNextLevel() method looks like Rick’s.

If it does, copy one of the following lines into your code

yield return null;
yield return new WaitForEndOfFrame();

and insert them in your code like this:

FindFirstObjectByType<ScenePersist>().ResetScenePersist();
// paste the copied line here
SceneManager.LoadScene(nextSceneIndex);

I hope this fixed it for you. :slight_smile:

Hi Nina,

Unfortunately, including this line did not seem to help. For reference, here is my LoadNextLevel() method:

I’m not sure if this is relevant, though I noticed another weird behavior with this issue. On my first level, I have one coin within my ScenePersist object. If I pick up up that coin, I can die on level one and the game resets like normal. If I don’t pick up that coin, dying on level 1 causes my window to freeze like it does on later levels.

If I pick up up that coin, I can die on level one and the game resets like normal.

Have you already tried to add Debug.Log to your code to see what’s going on? Do you run different code based on the number of collected coins?

Also, if a program freezes, the reason for this is usually an endless loop. This could happen if the code loads a level over and over again. I’m wondering how many components of the same type you have in your scene. Maybe the component that (re)loads a level is attached to the wrong game object(s).

For testing purposes, you could comment “suspicious” code parts out to see if you can identify the line where the problem starts.

In line 25, could you please hover your mouse over FindFirstObjectByType? Why is the method name crossed out? This could also be a potential source of unexpected behaviour.

I’ve tried adding a few debug statements in the relevant methods that effect ScenePersist and GameSession. None of them print before the loop occurs. I suppose if I have to, I could restructure all of those as coroutines with 1 frame delays so I can actually track it better…

After adding more objects to test, my first level has 2 coins and an enemy. I can now say that dying causes a freeze if not all 3 of these objects have been removed (when the ScenePersist only consists of empty folders for Pickups and Enemies).

As far as I understand, my code is structured the same way as Rick’s. While trying to debug this issue, I copy/pasted his GameSession, LevelExit, and ScenePersist scripts over mine and that did not fix it.

As far as the crossed out name on line 25, that’s just because it recommends FindAnyObjectByType() to not rely on ordering. I’ve tried both methods and it does not make a difference.

Finally, from my series of commenting lines, the issue goes away when I comment out line 61 of my GameSession code below, which destroys the ScenePersist object upon player death. Interestingly, this line remains active in LevelExit when I go to the next level, and that has not caused me any issues.

Good job on analysing your project. I think I need to take a look at it myself. Based on what you already figured out, I’m sure I’ll be able to narrow it down further. :slight_smile:

Could you please fill out our upload form? Your name, my name, the thread title and a link to your thread are important. You don’t have to describe the problem again. Zip your project but remove the Temp and the Library folder from it. Otherwise, the zip file will be too big, and I won’t be able to download it.

When you are done, please let me know here, so I can check our server.