Loose Collider Script not working

After Attaching the script to the loose collider and hitting the play, the ball keeps falling without loading to the ‘game over scene’

Here is my Loose Collider Code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class LoseCollider : MonoBehaviour
{
   private void OntriggerEnter2D(Collider2D collision)
    {
        SceneManager.LoadScene("Game Over");
    }
}

And this the Scene Loader

code

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;

public class SceneLoader : MonoBehaviour
{
   public void LoadNextScene ()
    {
        int currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
        SceneManager.LoadScene(currentSceneIndex + 1);
    }

    public void LoadStartScene()
    {
        SceneManager.LoadScene(0);
    }

    public void QuitGame()
    {
        Application.Quit();
    }
}

Hi Zulfikar,

Welcome to our community! :slight_smile:

Unity’s physic engine is highly optimised meaning it checks the position of a collider occasionally only. You could try to set the Collision Detection mode of the ball’s Rigidbody2D to “Continuous”. In most cases, that fixes the problem.

In our particular case, the problem is that we manipulate the transform.position via code and override the calculated values of the physics engine. It is likely that the position of the ball does not match the position of the collider anymore.

A better solution would be to disable the physics simulation for the ball while manipulating the position via code.

void Start ()
{
    paddleToBallVector = transform.position - paddle1.transform.position;
    myAudioSource = GetComponent<AudioSource>();
    myRigidBody2D = GetComponent<Rigidbody2D>();
    myRigidBody2D.simulated = false; // <-------- add this
}
 
private void LaunchOnMouseClick()
{
    if (Input.GetMouseButtonDown(0))
    {
        hasStarted = true;
        myRigidBody2D.velocity = new Vector2(xPush, yPush);
        myRigidBody2D.simulated = true; // <-------- add this
    }
}

Did this fix it?


See also:

Do I have to attach this code to ball?
If so, then I am getting the error - ’ the name ‘myRigidBody2D’ does not exist in the current context’

You have to replace the code inside your Ball.cs with this code. Be careful that you do not replace too much. I commented the relevant lines with “add this”.

I am really sorry to annoy you, but can you look at this
(https://drive.google.com/file/d/1tHFijQLaCbcfWU16FtrVPXUZ4vaxkFFZ/view?usp=sharing)
I did exactly the same as the lectures and yet the ‘GameOver’ scene doesn’t load and the ball keeps falling down on hitting the play button, when actually after reaching the loose collider it should load the game over screen.

I’m afraid with more than 250,000 students to support, I do not have time to download any projects unless I explicitely requested to send me a link to the project.

Check your Build Settings. Are all your scenes included in the list? “Game Over” should be at index 0. Also check the spelling of the method names and add a Debug.Log to the OnTrigger or OnCollision method to see whether it gets called. Remember you can also look at the lecture code changes via the link in the Resources of each lecture.

If you are in a hurry, please feel free to ask our helpful community of students over on our Discord chat server.

Thanks a lot. My game over was scene 2 in build settings. I’m able to continue with the course now. Thank you for sticking with me.

You’re welcome. I’m glad the solution was so simple. Have a nice weekend. :slight_smile:

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

Privacy & Terms