LoadNextScene Method is Unused?

Hello Again,

In my script method LoadNextScene seems unused. However we used it with invoke. Is it considerin unused because we used it with string name ? I am little bit confused.

LoadNextScene() is definitely being used. line 48 calls it. The reason it is being called with Invoke is because Invoke has that second parameter that delays the call of the method. In your implementation, it waits 1 second before calling LoadNextScene(). If you wanted to call it immediately, you could replace line 48 with:

LoadNextScene();

It’s just two different ways of calling the same method. Invoke calls it after a delay, the other way calls it immediately. In either case, the answer to your question is, the LoadNextScene() method is being used.

1 Like

You’re correct. When Visual Studio looks for references to a function, it’s looking for explicit calls to that function. When you call Invoke, you’re passing a string that (I assume) Unity uses to find a reference to the function that should fire after the specified time has passed. The string with the function name is not the same as an actual reference to that function, so Visual Studio doesn’t detect any references to LoadNextScene() or LoadDeathScene(), even though Unity calls them at runtime when the Invoke fires.

This is another argument against using strings to represent things that aren’t actually strings. If someone were refactoring this code and didn’t see the Invoke calls, they might think the LoadNextScene() function is just some leftover code from a previous revision and is no longer necessary because “it has no references”. They might think they’re justified in deleting it. If they do that, the game blows up without throwing any compiler errors. It’s a little trivial here, but it can easily happen in larger projects, and these errors can be a pain to track down.

1 Like

Thanks John and Seth :slight_smile:

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms