Collision not working

Hi there I am currently working on the rocket boost program by Rick Davidson.
And after implementing the Debug/Cheat Codes when I start my Game it won`t recognize any Collision at all.
Even without pressing the C Key.
Here is my Code:

using UnityEngine;
using UnityEngine.SceneManagement;

public class CollisionHandler : MonoBehaviour

{
AudioSource audioSource;

[SerializeField] float invokeTime = 1f;
[SerializeField] AudioClip obstacleCollission;
[SerializeField] AudioClip reachedFinish;

[SerializeField] ParticleSystem collissionParticles;
[SerializeField] ParticleSystem finishParticles;

bool isTransitioning = false;
bool collisionDisabled = false;

// Start is called before the first frame update
void Start()
{
    audioSource = GetComponent<AudioSource>();
}
void Update()
{
    RespondToDebugKeys();
}

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


    void OnCollisionEnter(Collision other)
    {
        if (isTransitioning || collisionDisabled) { return; }

        switch (other.gameObject.tag)
        {
            case "Friendly":
                Debug.Log("This Object is friendly");
                break;
            // Fuel will not be used
            case "Fuel":
                Debug.Log("This is Fuel");
                break;
            case "Finish":
                StartFinishSequence();
                break;
            default:
                StartCrashSequence();
                break;
        }
    }

    void StartFinishSequence()
    {
        isTransitioning = true;
        audioSource.Stop();
        audioSource.PlayOneShot(reachedFinish);
        finishParticles.Play();
        GetComponent<Movement>().enabled = false;
        Invoke("LoadNextLevel", invokeTime);
    }

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

    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);
    }
}

}

Hi Lerithian,

Welcome to our community! :slight_smile:

Could it be that the OnCollisionEnter method is declared within the code block of another method? Check the curly brackets. Unity’s methods must be declared at class/instance level and must not be wrapped in the code-blocks of other methods.

Remember you can also look at the lecture code changes via the link in the Resources of each lecture.

I hope this helped. :slight_smile:


See also:

Oh my God yes! It was one tiny little curly Bracket and I sat here rewatching my and Ricks Code over and over and wondered what I did wrong.
Thank you very much.

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

Privacy & Terms