My Collisions wont turn back on!

Hello, I was following the instruction on the debug lecture to the T and I seem to be stuck on turning my collisions back on. I know the code works since I can change the levels but I cannot figure out why the collisions aren’t working anymore.

public class CollisonHandler : MonoBehaviour
{
    //PARAMATERS
    [SerializeField] float LevelDelay = 2f;
    [SerializeField] AudioClip Success;
    [SerializeField] AudioClip Crash;
    [SerializeField] ParticleSystem ParticleSuccess;
    [SerializeField] ParticleSystem ParticleCrash;


    //CACHE
    AudioSource audioSource;

    //STATE
    bool isTransitioning = false;
    bool collisionDisabled = false;


    void Start()
    {
        audioSource = GetComponent<AudioSource>();
    }

    void Update()
    {
        RespondToDebugKeys();
    }

    void RespondToDebugKeys()
    {
        if (Input.GetKeyDown(KeyCode.L))
        { 
            LoadNextLevel();
        }
        else if (Input.GetKeyDown(KeyCode.C))
        {
            collisionDisabled = !collisionDisabled;
        }
    }

    void OnCollison(Collision Other)
    {
       if (isTransitioning || collisionDisabled) 
        {
            return;
        }
        switch (Other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("This thing is friendly");
                break;
            case "Finish":
                StartSuccessSequence();
                break ;
            case "Fuel":
                Debug.Log("Fuel Added");
                break;
            default:
                StartCrashSequence();  
                break;
        }
    }

    void StartSuccessSequence()
    {
       isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(Success);
        ParticleSuccess.Play();
        GetComponent<Movement>().enabled = false;
       Invoke("LoadNextLevel", LevelDelay);

      
    }
    void StartCrashSequence()
    {
        isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(Crash);
        ParticleCrash.Play();
        GetComponent<Movement>().enabled = false;
        Invoke("ReloadLevel", LevelDelay);
    }

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

}

Please if anyone one could point me in the right direction I would greatly appreciate it! Great course so far!

Welcome to GameDev Community!

Hmmm…lemme step through this…

  • So in the Update() method, you’ve got a reference to a RespondToDebugKeys() method…check.

  • In the RespondToDebugKeys() method you have an ‘else if’ to use ‘C’ to toggle collisionDisabled
    **I’m not sure if this needs to be an ‘else if’. You could probably use two different ‘if’ statements instead here. They might have done it this way in the tutorials just to show you something, but I think I’d try doing it differently. To be clear, they should both be inside the RespondToDebugKeys() method, but the ‘L’ should be probably one ‘if’ statement and the ‘C’ should probably have it’s own ‘if’ statement.

  • In the LoadNextLevel() method, you could probably add a line that says something like collisionDisabled = False.

Seriously though, this is difficult to read and interpret. Like !collisionDisabled is like a double negative and I think I would change it to collisionEnabled = true or false, then !collisionEnabled would be easier to read. Not sure why they did it the way they did.

I would try putting a collisionEnabled = true; in the LoadNextLevel() method. That should ensure the collision is re-enabled when the next scene loads.

The switch case statement is a little confusing, and now I can see why people hate them so much. I’ve never tried to read someone else’s Switch case statements before, but it appears that if you collide with anything but the above specified tags, it will StartCrashSequence(). That much looks okay.

Hmmm…I think your ‘C’ button should be something more like:

if (Input.GetKeyDown(KeyCode.L))
{
LoadNextLevel();
}

…and…

if (Input.GetKeyDown(KeyCode.C))
{
if (collisionEnabled) {
collisionEnabled = false;
} else {
collisionEnabled = true;
}
}

This would allow for the ‘C’ button to toggle the collision instead of just turning it on. I know I changed your variables around, but it makes more sense to me this way.

I hope this helps. If not, we’ll see what happens and try something else.

1 Like

Yeah this is very odd for me since I even used the gitlab refrence of the lecture. I tried your solutions and I’m unfortunately still unable to see myself out of this jam.`void Update()
{
RespondToDebugKeys();
}

void RespondToDebugKeys()
{
    if (Input.GetKeyDown(KeyCode.L))
    { 
        LoadNextLevel();
        Debug.Log("Next Level")
    }
    
    if (Input.GetKeyDown(KeyCode.C))
    {
        if (collisionEnabled)
        {
            collisionEnabled = false;
        }
        else
        {
            collisionEnabled = true;
        }
        Debug.Log("Collision Toggle");
    }
}`
 void LoadNextLevel()
    {
        collisionEnabled = true;
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        int nextSceneIndex = currentSceneIndex + 1;
        if (nextSceneIndex == SceneManager.sceneCountInBuildSettings)
        {
            nextSceneIndex = 0;
        }
        SceneManager.LoadScene(nextSceneIndex);
    }   

I really appreciate your help with this btw, even if I don’t get this right! I was fiddling with this for days and I finally caved in and asked the community and you guys are awesome, seriously!

1 Like

Is this from Project Boost or a different game?

1 Like

Yes it is. great course so far since I am coming from zero experience and I have learned so much!

1 Like

You’ll get there, after a few hard drive crashes and OS reinstalls, you’ll be blasting through projects a second and third time like they’re nothing! :laughing:

Seriously though, it takes time. It took me months to really get the hang of methods, classes and inheritance, so don’t worry if you feel like things are throwing you for a loop. It happens to all of us.

It’s been a long time since I did the project boost game, so I don’t remember it very clearly. I don’t think I was using GitHub at the time, so I lost everything during a crash.

I hope someone else will be able to take a closer look and maybe give you some better advice.

I’ll check to see if the reference code is posted somewhere. I might be able to tell you more after I see the instructor code.

Which lesson, specifically, are you working on for this issue? So I can try to find it faster?

1 Like

Here is one thing I see…

Rick has a lowercase ‘o’ in his switch statement.

Yours is capital. Try that out and see what happens. I’ll check more of the repo for anything else I can find.

1 Like

more errors

In here, there are a few mismatches:

  • ‘success’ is not capitalized in Rick’s PlayOneShot(success) code
  • Rick uses successParticles - You wrote ParticleSuccess
  • Rick wrote levelLoadDelay - You wrote LevelDelay

An important thing to remember in C# is that variables start with a lower case letter, not a capital. There are other characters that can also be used to begin a variable, but capital letters have a specific use case for things like methods and classes.

  • Rick’s PlayOneShot uses ‘crash’ - You wrote ‘Crash’
  • Rick wrote crashParticles.Play() - you wrote ParticleCrash.Play()
  • levelLoadDelay - same as above
1 Like

I’ve tried but seem to see no difference.

okay I will try this!

I think this may be one that could just be a bug with unity? I will look into the course lecture for debug keys in gamedev forum. I found some interesting variations from the other members and that might just be where I’ll find my solution! Thank you so much for your time!

Your script has a mis-spelling of ‘Collision’, which may or may not be important.

I see that most of the other tings I pointed out correspond to the way you named your variables, but it would still be best to stick with those best practices and label them the way that Rick does, at least for the time being.

I think it would be a good idea for you to really read through Rick’s code and scrub it against your own code. If you’re going to be a programmer, you need to get used to doing this. I know it seems pretty tedious, but you’ll get good at it and you’ll start seeing inconsistencies like this from a mile away. If you want to be a game developer, this is what you signed up for.

That said, ignore my previous suggestions about the ‘if’ statements and other deviations from the original code. Follow what Rick does (even if !collisionDisabled is semantically goofy).

I hope this helps.

1 Like

It does help and yes this is something I am willing to purposefully spend my time learning. Thank you for the advice for scrubbing code, never heard that before anywhere else but I am always looking to learn best practice from anyone with experience and you helping me with yours is very generous!

1 Like

You’re welcome. You’ll get there.

1 Like

So I just tried your advice to scrub my code which led me to find a few misspellings and also in the OnCollisionEnter method I spelled as OnCollision. So, after fixing it to how Rick had his code all is good.

1 Like

Good Jorb!

Code reviews are helpful, but nothing can replace proofreading your own work.

That is to say, don’t be upset when someone is reviewing your code and they find enough inconsistencies and they get to a point where they say, “Hey, you really need to do this yourself.”

Some people really don’t like to proofread, but it is one of the most necessary skills in programming as you can see. It’s definitely a skill that is important to cultivate.

Personally, I practice proofreading almost everything I write, even posts in here. For whatever reason, I enjoy Technical Writing. I think is part of my psychosis. I’ll find myself reading a post like this one several times over and I find all kinds of mistakes and correct them…but that also helps me when I go to review my own code.

Anyway, it seems like you’re back on track. Way to go!

goodjorb

2 Likes

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

Privacy & Terms