Help with my code?

Hi there,
I’ve been struggling with my code and I can’t find the reason for it to not work. After hours of trying to fix it I decided to ask for some help.
The issue is that when I land on the Landing Pad it says:

Trying to Invoke method: CollisionHandler.LoadNextlevel couldn’t be called.

StartSuccessSequence() is pretty much the same thing as StartCrashSequence() , however it doesn’t work for some reason. I’ve checked my curly brackets and they seem fine. Here’s the code:

using System;
using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour
{
[SerializeField] float crashDelayTimer = 1f;
[SerializeField] float loadDelayTimer = 1f;

void OnCollisionEnter(Collision other) 
{
    
    switch (other.gameObject.tag)
    {
        case "Friendly":
            Debug.Log("This thing is friendly");
            break;
        
        case "Finish":
            StartSuccessSequence();
            break;
        
        default:
            StartCrashSequence();
            break;
    }
}
void StartSuccessSequence()
{       
    GetComponent<Movement>().enabled = false;
    Invoke("LoadNextlevel", loadDelayTimer);
}
    
void StartCrashSequence()
{
    GetComponent<Movement>().enabled = false;
    Invoke("ReloadLevel", crashDelayTimer);
}

void ReloadLevel()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    SceneManager.LoadScene(currentSceneIndex);
}

void LoadNextLevel()
{
    int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    int nextSceneIndex = currentSceneIndex + 1;
    if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
    {
        nextSceneIndex = 0;
    }
    SceneManager.LoadScene(nextSceneIndex);
}

}

Any help would be appreciated!

1 Like

Are your scenes added to the build?

2 Likes

Yep, they are.

2 Likes

I agree with @Maverick1868. If all of your scenes are not added into the Build Settings screen, there won’t be a build index.

You could also put a

Debug.Log("De-doo-doo-doo, De-daa-daa-daa");

inside your LoadNextLevel() method to see if it is firing at all. If you put the Debug message above the rest of your other code, it should trigger before the error is thrown and you should see it in the console.

Another troubleshooting tip I use a lot is to comment out each line of code one by one until the error clears…or I’ll comment out everything and add back one line at a time until I get the error again.

Finally, the error message will tell you exactly which line that Unity think has the problem. Can you post a screen grab of the error message from the console?

Back to what @Maverick1868 is suggesting, if you added your start level but forgot to add level 2 into your build settings, your game will start but there will not be a buildIndex[1] to change over to…which I agree would likely be the cause.

2 Likes

I’ve tested the method and played with it a bit.
GetComponent().enabled = false; works fine, the object stops moving. It just doesn’t recognise the “LoadNextLevel” string part.

image

image

I’ve just fixed it! it was a typo because of course it was… T.T
“Invoke(“LoadNextlevel”, loadDelayTimer);” with a lowercase l instead of L.
I’m so sorry! And thanks for the fast replies, I really appreciate it. :slight_smile:

1 Like

Nothing to apologize for, we’ve all done it. It’s part of the learning process.

Yup…
Screenshot from 2022-07-29 22-29-06

There it is. Level is not capitalized.

Screenshot from 2022-07-29 22-30-22

Good job! I was comparing your code to Rick’s and I missed that one, too.

The hardest problems always seem to be the most fundamental.

Game on! Oh, and welcome to GameDev Community!

1 Like

Yess! No worries. That’s the way it is and sometimes a fast reply can make all the difference whether you have the motivation to press on or just call it quits.

Welcome to the community @JellyHamster

C# has introduced nameof a while ago to prevent just this issue. It is safer to use

Invoke(nameof(LoadNextLevel), loadDelayTimer);

With this you will have intellisense and the compiler will fail at design time if the function does not exist (as in your case).

2 Likes

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

Privacy & Terms