Creating A DeathHandler Class

Any questions about our DeathHandler?

The mouse unlocked didnā€™t work in my project. Well, you could see the mouse an instant, but the FirstPersonControler blocked it again, even when the timeScale was zero. It seems the MouseLook script in the FirstPerson asset doesnā€™t freeze up. So I unchecked the LockCursor in the FirstPersonControler, and blocked it in the SceneLoader, but it only works after you die a first timeā€¦

Ok, a better solution (or so I think!), disable the FirstPersonController in the DeadHandle Script:

ā€¦
GetComponent().enabled = false;
ā€¦

1 Like

Hi Rick and All,

Firstly, really loving this part of the course. But I had a heap of problems with this one. Mainly, the Try Again button would only work randomly.

It seemed the First Person Controller was causing issues. Disabling it was not straight forward either, because it seems as part of the standard asset pack it has its own namespace.

But I finally came up with the answer, you need to add the 'using UnityStandardAssets.Characters.FirstPerson; ā€™ at the top of the script.

Disabling the First Person Controller also means that you donā€™t need to use ā€˜Time.timeScale = 0ā€™, which might be fun later to watch the enemies keep moving after youā€™re dead. But then you might need to disable the Weapon Script.

I also didnā€™t like that the gun was still showing when the death canvas came up. The gun is made up of a heap of components so I disabled it with a for each loop. Check out my code below.

I hope this helps.
Cheers
James

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class DeathHandler : MonoBehaviour
{
[SerializeField] Canvas gameOverCanvas;

void Start()
{
    gameOverCanvas.enabled = false;

}

public void HandleDeath()
{
    GetComponent<FirstPersonController>().enabled = false;

    MeshRenderer[] meshRenderers = GetComponentsInChildren<MeshRenderer>();

    foreach (MeshRenderer mesh in meshRenderers)
    {
        mesh.enabled = false;
    }

    Time.timeScale = 0;
    Cursor.lockState = CursorLockMode.None;
    Cursor.visible = true;
    gameOverCanvas.enabled = true;
}

}

3 Likes

Hi Rick and all,

Iā€™m having a devil of a time with this bit. I have moved past it accepting that I have to live with a buggy UI for the moment. Currently, I have followed the video series word for word regarding the death handler and the associated UI, however, when HandleDeath() gets called, time stops, the mouse pops out, but the UI doesnā€™t render until I restart the game. And then, it wonā€™t go away until I stop and start the game again. Here is my current script, any help would be greatly appreciated. I have tried disabling the FirstPersonCamera to see if the error was coming from Time.timeScale, but my attempts were fruitless as the UI still refused to render.

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

public class DeathHandler : MonoBehaviour
{
[SerializeField] Canvas gameOverCanvas;

private void Start()
{
    gameOverCanvas.enabled = false;
}

public void HandleDeath()
{
    gameOverCanvas.enabled = true;
    Time.timeScale = 0;
    Cursor.lockState = CursorLockMode.None;
    Cursor.visible = true;
}

}

Cheers
Scott

1 Like

Solved my issue. Changed my gameOverCanvas reference to a GameObject and used .SetActive(). For whatever reason, that worked while .enabled did not.

thanks so much for this!
I had the same problem on the previous lecture, and just posted a response citing this post as a solution.

I also had a problem where the cursor wouldnā€™t unlock and be visible, and adding this line of code in DeathHandler.HandleDeath() just before the Cursor methods fixed it:

GetComponent<UnityStandardAssets.Characters.FirstPerson.FirstPersonController>().enabled = false;

This is basically what others are saying above but just requires the one additional line.

3 Likes

It worked thanks!

hello everyone,
i have some weird issue.
after clicking on play again button it reloads the scene with darker lightning.
i have turned off auto generate lightning but still itā€™s happing, and my game environment is about day time so thatā€™s why i didnā€™t made the changes that rick did for darker environment.
really need some help.

Hey, was having issues with it too. Thanks for help!

Hi Rick, I am having problems with my player. I cannot add the script to it. It gives an error with states ā€œYou are trying to replace or create a Prefab from the instance ā€˜Carbineā€™ that references a missing script. This is not allowed.ā€ I did everything I could think of, but I cannot find the solution.

Any ideas how to solve same issue with Unity Starter assets for first person as standard assets arenā€™t a thing anymore? @Rick_Davidson

This one was brutal. I tried a bunch of different solutions from the UDemy comments that didnā€™t work.

I finally created a second Camera that starts out disabled with an overhead view.

So what I ended up doing was:

  • Set the Cursor visible and LockMode to None
  • Disabled the entire Player object
  • Enable the Game Over canvas
  • Enable the second camera (Which I called the ā€˜overview cameraā€™)
  • Then when the restart is clicked, I turn the CursorLockMode back to Locked

Not exactly the same result as the class, but it worked for me. Iā€™m making more of a startegy shooter, so an overhead shot works. But a camera at the start point would probably also make sense.

  public void DisplayGameOverMenu()
	{
      Cursor.lockState = CursorLockMode.None;
      Cursor.visible = true;

      gameOverCanvas.SetActive(true);
      player.SetActive(false);
      overviewCamera.SetActive(true);
   }

   public void RestartGame()
	{
      Scene currentscene = SceneManager.GetActiveScene();
      SceneManager.LoadScene(currentscene.buildIndex);

      Cursor.lockState = CursorLockMode.Locked;
   }

Iā€™m using this asset pack which requires a bit of creative rework from the original course version:

Iā€™ve got a wierd situation that iā€™m not sure how to solve.
When the player dies, the screen freezes as we wanted but the movement of the mouse still moves the camera. How can I fix that?
If it helps, when Iā€™m pressing one of the buttons the player still shoots. I can see that by the consol comments of the ā€œI hit this thing: ā€¦ā€

Privacy & Terms