Why cant my script load? (783943)

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



public class LoseCollider : MonoBehaviour
{

    private void OnTriggerEnter(Collider2D collision)


    {


        SceneManager.LoadScene("GameOver");


    }


}

Hi,

Please use code tags. Not much extra effort needed for that. You can also edit the post you’ve sent.

For starters, does your object that is colliding have a Rigidbody applied? One of the objects in the collision detection setup has to have a Rigidbody. Or Rigidbody2D if it’s 2D.

Ok

I have rigidbody 2d on. What code tags should i use instead of what?

code tags .

Code tags are used to make code look like something closer to an IDE etc. And generally just readable.

See the editor button when you edit your message.

5673079--591385--code_tags.PNG

The result looks like this:

public bool thisIsCodeTags = true;

ok now

To narrow down the problem solving, use debug logs. You can figure out this in minute or two with correct tools. There aren’t many things that can be wrong in this case.

Put a Debug.Log(“Collision triggered.”); in that OnCollisionEnter. Then you instantly see if any collision trigger when you drag a GameObject to that trigger.

If no collision happens, then check if you have collider, then check that either of the colliding items has a RigidBody.

If you have both, then check that the collision matrix is correct in the project settings. Unity - Manual: Layer-based collision detection

ok like this

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



public class LoseCollider : MonoBehaviour
{

    private void OnTriggerEnter(Collider2D collision)
    {
      
        SceneManager.LoadScene("GameOver");

        Debug.Log("Collision triggered.");

    }


}

I’d skip the loading part for now. First you have to figure out why the trigger doesn’t get triggered. Always solve problems one by one. Don’t add more code if a thing doesn’t function ok.

try “OnTriggerEnter2D” instead of “OnTriggerEnter”

Still dosent work

ok