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 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.
![]()
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