- Home /
Problem is not reproducible or outdated
Pause menu not working..
Hi, i have made a pause menu which slightly works... I want to disable the mouseLook and movement scripts on the player when they do pause.. this script is on an object called GameMaster so i am trying to access the player (where these components are..) but when i pause i can still move the camera around, etc..
if (pause == true && networkView.isMine) {
GameObject.FindWithTag("Player").GetComponent<CharacterController>().enabled = false;
GameObject.FindWithTag("Player").GetComponent<MouseLook>().enabled = false;
GameObject.FindWithTag("Player").GetComponent<CharacterMotor>().enabled = false;
}
else if (pause == false && networkView.isMine){
GameObject.FindWithTag("Player").GetComponent<CharacterController>().enabled = true;
GameObject.FindWithTag("Player").GetComponent<MouseLook>().enabled = true;
GameObject.FindWithTag("Player").GetComponent<CharacterMotor>().enabled = true;
}
Answer by sedativechunk · Apr 09, 2014 at 10:06 AM
Yes, there is a better way to do this. I've always had a good mind for how to do pause menus easily. One of the simpler ways as far as Unity goes is to create an empty game object called "activestate" for example. In this activestate game object, you can put EVERYTHING that runs your actual game, ex. the player, level, background, environment, etc.
When the player pauses the game, you can disable the entire state of this "activestate" game object and display another game object instead with your pause menu. If you use this method, then instead of tinkering with tons of different things and tags you only have to disable one single game object. When you reactivate it, everything in there should be as is when the state was last active (except objects that use the "awake" functions may need tweaked a bit).
Another kind of trick (although not anywhere nearly as effective) is to manipulate the time. If you programmed your game correctly, most of the actions/movements/physics are multiplied by "Time.DeltaTime" or "Time.Time". You could maybe modify those values to multiple by 0 when in pause state. Then the game will "freeze" in action. Just another interesting way to go about it. I prefer the previous method I mentioned.
note, that this game is multiplayer, so I thought that just disabling the scripts would be best suit for it.
I think you could still use this method if you are doing multiplayer, you need to have a script outside of the disabled game objects to handle it. That might even be your problem now, if you are disabling anything that is handling the networking then it's going to cause a problem.
@pako - only for the player that pauses. I just want it so they can free their mouse and change settings, etc without their camera moving.
@sedativechunk - technically this is the outside script. Im trying to access the players scripts and disable them. This is the Game$$anonymous$$aster script. the pause menu is just a small box with a few buttons. If the other player walks by, I want them to still see them walk by.. Just not be able to move. So again, slowing speed down still wont work for it.
saying this:
if (pause == true && networkView.is$$anonymous$$ine) {
GameObject.FindWithTag("Player").GetComponent<CharacterController>().enabled = false;
}
looks logical, and should technically work; but I think im just missing something slightly thats causing the whole thing not to work.
this is the error im getting:
NullReferenceException
UnityEngine.GameObject.GetComponent[CharacterController] ()
Game$$anonymous$$aster.Update () (at Assets/Game$$anonymous$$aster.cs:116)
Still no working solution, sadly. The above error is still occurring.
Follow this Question
Related Questions
I am having a parsing error in a C# pause script, can anyone help? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unexpected Strange Behavior 0 Answers
Code Problem 1 Answer