Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to stop first person controller (from standard asssets) move when game over condition?

Discussion in 'Scripting' started by AdamW99, Jan 5, 2021.

  1. AdamW99

    AdamW99

    Joined:
    Nov 10, 2020
    Posts:
    5
    I have a question about first person controller, which i have from standard assets. How can i stop moving this controller after game over condition in my game?
    That is after my health points are 0 i want to stay in one place and cant walk, jump, etc, and next i will set my game-over screen. I think that i must set something in script and function death() but how to do that with fpc controller?

    Thanks for help!
     
  2. Terraya

    Terraya

    Joined:
    Mar 8, 2018
    Posts:
    646
    to keep it very very simple, disable the Component after Death
     
  3. AdamW99

    AdamW99

    Joined:
    Nov 10, 2020
    Posts:
    5
    But when i set the object: public static FirstPersonController fpc; and then make: fpc.enabled=false; in my death function i get: NullReferenceException: Object reference not set to an instance of an object.
    Is there any other option to solve this problem or maybe i turn it off wrong?
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,768
    Then check it before you use it, if you are obviously expecting it to be Destroyed (which you are expecting)!!

    In any case, regardless of your final engineering choices above, the answer is always the same... ALWAYS. It is the single most common error ever. Don't waste your life on this problem. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  5. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    You can put the input checks in an if condition, so the character will only move if the game is not over. You'd want to do the same thing with the mouse look.

    if (!gameOver) {
    if (Input.GetKeyDown()) // etc

    Or you can set the move, rotate, and jump parameters to 0 ;)
     
  6. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Timescale to 0 will pause all movement :)
     
    seejayjames likes this.
  7. seejayjames

    seejayjames

    Joined:
    Jan 28, 2013
    Posts:
    685
    Agreed, that's the easiest. The only issue is if you still want other objects moving visually ("the world moves on") even when the character is dead, it's just a design decision.
     
  8. MDADigital

    MDADigital

    Joined:
    Apr 18, 2020
    Posts:
    2,198
    Yeah in our game we disabled interaction and movement and grayed the screen for 5 seconds before returning player to load out / spawn select. If your controller is well designed it shouldn't be a hard task
     
    seejayjames likes this.