Damage Collider not triggering "Lose Screen"

Hello, I’m having an issue with the collider not triggering the “lose screen”. The game is designed in such a way that if an enemy crosses over the collider at the left end of the screen and the player’s health reaches zero, the “lose screen” will trigger. I have verified my code in the Github repository but I still have no trigger and no console error or anything. I rewatched the video but I’ve done everything I’m supposed to. I’ll paste my code anyway, just in case.

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

public class Level : MonoBehaviour
{
[SerializeField] int timeToWait = 4;
int currentSceneIndex;

void Start()
{
    currentSceneIndex = SceneManager.GetActiveScene().buildIndex;
    if (currentSceneIndex == 0)
    {
        StartCoroutine(WaitForTime());
    }
}

IEnumerator WaitForTime()
{
    yield return new WaitForSeconds(timeToWait);
    LoadNextScene();
}

public void LoadNextScene()
{
    SceneManager.LoadScene(currentSceneIndex + 1);
}
public void LoadYouLose()
{
    SceneManager.LoadScene("Lose Screen");
}

}

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

public class DamageCollider : MonoBehaviour
{
private void OnTriggerEnter2D()
{
FindObjectOfType().TakeLife();
}
}

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class Lives : MonoBehaviour
{
[SerializeField] int lives = 5;
[SerializeField] int damage = 1;
Text livesText;

void Start()
{
    livesText = GetComponent<Text>();
    UpdateDisplay();
}
private void UpdateDisplay()
{
    livesText.text = lives.ToString();
}

public void TakeLife()
{
    lives -= damage;
    UpdateDisplay();

    if (lives <= 0)
    {
        FindObjectOfType<Level>().LoadYouLose();
    }
}

}

Your code looks fine.

If you are not getting any error message then there’s something else going on here, like the collider not detecting the collision. Do your enemies have a Rigidbody2D attached to them? If not, then there’s your problem. Also check the collider that triggers the lose, it’s a very common mistake to use a Box Collider instead of a Box Collider 2D.

1 Like

Got the Rigidbody 2Ds on the attackers. The enemies trigger the loss of life, the game just doesn’t go to the lose window after life is at 0. It’s in the code but it’s still not working. I have the scripts attached where there supposed to be. He put the Level script on the game area object and the damage collider on the box collider 2d. Its just not triggering and I can’t figure out why. The collider isn’t supposed to be a trigger so that is unchecked. I just don’t get it :confused:

MMmmm… I just noticed something weird with the code you posted:

private void OnTriggerEnter2D()
{
    FindObjectOfType().TakeLife();
}

That code should not work, it’s supposed to be:

private void OnTriggerEnter2D()
{
    FindObjectOfType<Lives>().TakeLife();
}

But that should prevent the game from running and an error should appear in the console. There’s a small chance you didn’t save the file, so it’s running without the error because it hasn’t compiled yet, but according to you the player does lose lives, so it must be something else.

Another thing that might be causing this issue is that maybe there’s no Lose Screen in the Build Settings, but that would also send an error message.

According to the details of your post, there’s no way an error like the one you describe could occur but also your code should not be working at all, so there’s something weird here, if you can copy-paste your code again that would be great, but preferably, create a package with your whole project and upload it for me to download and see the whole code.

1 Like

Glitch Garden

This should be a google drive link to my project. The code didn’t get copied correctly. I tried to fix it earlier but was unable to. It does have the ().TakeLife(); in there and you should be able to see that.

Anyway, I really appreciate the help :slight_smile:

grr… it has the right format like you’re suggesting. I just am not familiar enough with utilizing this forum to write it correctly here.

The folders are empty :worried:

I suggest you import it as a package, it’s a single file so it’s easier to upload. To do that just right click on the Assets Folder, then Export Package, a new window will open, click on Export, it will create a Unity Package file, upload that:

1 Like

https://drive.google.com/file/d/1kVR7xxu5MKJR2pfgvOa6vDTW2pvM0UM6/view?usp=sharing

kk did that. Hopefully you can download that

1 Like

Ok, here’s what is going on.

The names in your code are a little messy and that’s what causing the issue: Look at this:

See the name and the class name:

  • File name Lives, class name LivesDisplay
  • File name LivesDisplay, class name Lives

That’s is why you are not getting any errors, your code never calls LoadYouLose because the class you are using doesn’t have those commands, it’s a little confusing. So here’s how to fix it:

Delete the “Lives” script.
Captura de Pantalla 2020-12-22 a la(s) 20.47.38

Open your “LivesDisplay” script and change the class name to “LivesDisplay”.
Captura de Pantalla 2020-12-22 a la(s) 20.49.41 Captura de Pantalla 2020-12-22 a la(s) 20.50.21

And that’s it, problem solved.

You’re a friggin’ genius. I’m about to crash but I’ll fix this first thing tomorrow morning. I’ll bet that’s it but I’ll try it tomorrow and update the thread. Thanks again, Superman!

1 Like

That got it. Thanks again bud :smiley:

1 Like

Is the issue fixed, @Wakeofchaos?


See also:

Glad I was able to help!

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

Privacy & Terms