#Hey can anyone help me create a code for
1 messages · Page 1 of 1 (latest)
im making a 3d runner and the player needs to freeze when i press the pause button and i cant find out how to disable the script of the player running when i press pause
This is where im up to
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class PauseMenu : MonoBehaviour
{
[SerializeField] GameObject pauseMenu;
public void Pause()
{
pauseMenu.SetActive(true);
}
public void Home()
{
SceneManager.LoadScene("MainMenu");
}
public void Resume()
{
pauseMenu.SetActive(false);
}
}
It may not be the best way to do it but if you want to disable the script, you use:
enabled = false;
ill try it
it might be better to have a paused variable somewhere your player can check in update() and return before doing any work.
yeah
so ive added this little bit at the bottom but it said that "Player Movement is a type but not a vairiable"
public void Pause()
{
pauseMenu.SetActive(true);
PlayerMovement = false;
}
you need the instance of PlayerMovement used by your player
so how would i make PlayerMovement a vairiable
you can have a serialized field and assign this in your scene or use FindGameObjectEtc..
Do you create/destroy your player during gameplay?
i have tried using serialize field but i kept on getting errors
no
Then put;
ok
public YourMovementClassName thePlayer; at the top
and use thePlayer.enabled = false;
ooo ok
then assign the player to this in your scene just like you did for the pause menu UI
ok
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class PauseMenu : MonoBehaviour
public PlayerMovement ThePlayer;
{
[SerializeField] GameObject pauseMenu;
public void Pause()
{
pauseMenu.SetActive(true);
Player.Enabled=false;
}
public void Home()
{
SceneManager.LoadScene("MainMenu");
}
public void Resume()
{
pauseMenu.SetActive(false);
}
}
im still getting many errors any other thing maybe i did smthin wrong
ohh
public class PauseMenu : MonoBehaviour
- public PlayerMovement ThePlayer;
{
public class PauseMenu : MonoBehaviour
{
public PlayerMovement ThePlayer;
ohh ok
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class PauseMenu : MonoBehaviour
{
public PlayerMovement ThePlayer;
}
{
[SerializeField] GameObject pauseMenu;
public void Pause()
{
pauseMenu.SetActive(true);
Player.Enabled=false;
}
public void Home()
{
SceneManager.LoadScene("MainMenu");
}
public void Resume()
{
pauseMenu.SetActive(false);
}
}
is it this
I also reckon .enabled is lowercase,
If you have your code editor properly set up you should get little red squigglies when something is mistyped. And if not, I think getting it set up would make things a lot easier for you :D
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class PauseMenu : MonoBehaviour
{
public PlayerMovement ThePlayer;
[SerializeField] GameObject pauseMenu;
public void Pause()
{
pauseMenu.SetActive(true);
Player.Enabled=false;
}
public void Home()
{
SceneManager.LoadScene("MainMenu");
}
public void Resume()
{
pauseMenu.SetActive(false);
}
}
ok thanks sm ill try it
ok thanks
YESSS i got no errors thanks sm
cool beans
so on the right ive got the script attached but it is not stopping the player
Are you calling the Pause() function from your pause button?
so i need the player to be able to attach a script to it
it should be
so on the right i need the PlayerMovement script to be there to disable it
You've already specified the script type (PlayerMovement) in the script, so the selection window will display all gameobjects with that script attached to them. All you need to do is select gorilla player from the list.
i have but it wont freeze the gorilla
Does your pause button gameobject actually invoke the Pause() function though?
And keep in mind that your Lava Monks also seemingly has a PlayerMovement script, which won't be disabled from your current script.
i know i will have to add the lava monks in but im just trying to get the first script right
What currently calls Pause()?
the pause scene
Show where you call Pause() from
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class PauseMenu : MonoBehaviour
{
public PlayerMovement Player;
[SerializeField] GameObject pauseMenu;
public void Pause()
{
pauseMenu.SetActive(true);
Player.enabled = false;
}
public void Home()
{
SceneManager.LoadScene("MainMenu");
}
public void Resume()
{
pauseMenu.SetActive(false);
}
}
this is my script
Show us a screenshot of the inspector window for your pause button gameobject. You need to have a button component and hook up the OnClick event to the Pause function.
im not sure
It doesn't matter what the function does if you never actually call it anywhere
pause Button, not pause menu ;)
ik
You have to make sure that clicking the button actually calls your new Pause() function.
Yeah that looks correct to me
Okay, there we go. Other than your apparently missing scripts which you should probably get rid of. Assuming that you dragged in the Pause Menu you showed in the previous screenshot, that should call the function when you click on it
I suspect PlayerMovement might only be in charge of player controls (left-right) and not the actual gameplay loop
so instead of pause menu i put paus button
No, your setup there looks right
no it has the player forward speed
And there's no other script under eg. "LevelControls" doing stuff?
( Might help if you post the PlayerMovement code too )
YESSSSSSS
I got it
Thanks so much guys
i had the script on the gorilla aswel so i got rid of it and it worked
so ive got the player stopping but ive done the exact same to the lava monks but it aint working any idea?
You might want to try a different approach to pausing that's gives you a bit more flexibility.
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class PauseMenu : MonoBehaviour
{
public static bool IsPaused = false;
[SerializeField] GameObject pauseMenu;
public void Pause()
{
pauseMenu.SetActive(true);
IsPaused = true;
}
public void Home() {
SceneManager.LoadScene("MainMenu");
}
public void Resume()
{
pauseMenu.SetActive(false);
IsPaused = false;
}
}
Then you simply do
public class MyOtherScript : MonoBehaviour
{
void Update()
{
if (PauseMenu.IsPaused) {
// By doing this IsPaused check here and calling return,
// the rest of the code in this method won't run
return;
}
// Other code (that won't run when the game is paused)
Blabla();
}
}```
👆 This way you can easily check for IsPaused in any method you want to not run when the game is paused.
Player.enabled will disable the whole script component.
IsPaused is just a variable we set to true or false, to indicate whether the game logic should run or not
can i ask what a bool dose
a bool is just true or false,
ohh k
so i have done the same thingto the lava monks but it is not working ive changed the script so it is not the asme a sthe player one but it isnt pausing the lava
i have a seprate script for each one naming them 1 2 3
using UnityEngine;
using UnityEngine.SceneManagement;
using System.Collections;
using System.Collections.Generic;
public class Lava1movepause : MonoBehaviour
{
public LavaMovement1 Lava;
[SerializeField] GameObject pauseMenu;
public void Pause()
{
pauseMenu.SetActive(true);
Lava.enabled = false;
}
public void Home()
{
SceneManager.LoadScene("MainMenu");
}
public void Resume()
{
pauseMenu.SetActive(false);
Lava.enabled = true;
}
}
why might this not be working
The honest and kinda boring answer here is because you don't really know what you're doing. I think learning by doing is great and I hope someone else chimes in ( I have to hop off )- But until then I'd really recommend you check out some beginner C# tutorials (not necessarily Unity related).
I really like Mosh' stuff :)
https://www.youtube.com/watch?v=gfkTfcpWqAY&list=PLTjRvDozrdlz3_FPXwb6lX_HoGXa09Yef
Learn C# basics in 1 hour! ⚡ This beginner-friendly tutorial gets you coding fast. No experience needed.
❤️ Join this channel to get access to perks:
https://www.youtube.com/channel/UCWv7vMbMWH4-V0ZXdmDpPBA/join
🚀 Ready to master C#?
- Check out my full course: https://mosh.link/csharp-course
- Subscribe for more awesome content: https://bit.l...