#Hey can anyone help me create a code for

1 messages · Page 1 of 1 (latest)

grave beacon
#

Hello! can you explain what button you want to press?

summer patio
#

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);

}

}

grave beacon
#

It may not be the best way to do it but if you want to disable the script, you use:

#

enabled = false;

summer patio
#

ill try it

grave beacon
#

it might be better to have a paused variable somewhere your player can check in update() and return before doing any work.

summer patio
#

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;
}

grave beacon
#

you need the instance of PlayerMovement used by your player

summer patio
#

so how would i make PlayerMovement a vairiable

grave beacon
#

you can have a serialized field and assign this in your scene or use FindGameObjectEtc..

#

Do you create/destroy your player during gameplay?

summer patio
#

i have tried using serialize field but i kept on getting errors

grave beacon
#

Then put;

summer patio
#

ok

grave beacon
#

public YourMovementClassName thePlayer; at the top

#

and use thePlayer.enabled = false;

summer patio
#

ooo ok

grave beacon
#

then assign the player to this in your scene just like you did for the pause menu UI

summer patio
#

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

grave beacon
#

too high

#

public PlayerMovement ThePlayer;

summer patio
#

ohh

grave beacon
#

this needs to go inside the class

#

after the {

summer patio
#

sorry where

#

so inside public void pause

grave beacon
#

public class PauseMenu : MonoBehaviour

  • public PlayerMovement ThePlayer;

{

#

public class PauseMenu : MonoBehaviour
{

#

public PlayerMovement ThePlayer;

summer patio
#

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

dense locust
#

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

grave beacon
#

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);

}

}

summer patio
#

ok thanks sm ill try it

summer patio
#

YESSS i got no errors thanks sm

grave beacon
#

cool beans

summer patio
#

so on the right ive got the script attached but it is not stopping the player

dense locust
#

Are you calling the Pause() function from your pause button?

summer patio
#

so i need the player to be able to attach a script to it

summer patio
#

so on the right i need the PlayerMovement script to be there to disable it

dense locust
#

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.

summer patio
#

i have but it wont freeze the gorilla

dense locust
#

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.

summer patio
#

i know i will have to add the lava monks in but im just trying to get the first script right

broken harness
#

What currently calls Pause()?

broken harness
#

Show where you call Pause() from

summer patio
#

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

broken harness
#

That doesn't answer me

#

Where do you call Pause()

dense locust
#

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.

summer patio
#

im not sure

broken harness
#

It doesn't matter what the function does if you never actually call it anywhere

summer patio
dense locust
summer patio
#

ik

dense locust
#

You have to make sure that clicking the button actually calls your new Pause() function.

summer patio
#

it dose

dense locust
#

Yeah that looks correct to me

broken harness
# summer patio

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

dense locust
#

I suspect PlayerMovement might only be in charge of player controls (left-right) and not the actual gameplay loop

summer patio
#

so instead of pause menu i put paus button

dense locust
#

No, your setup there looks right

summer patio
dense locust
#

And there's no other script under eg. "LevelControls" doing stuff?
( Might help if you post the PlayerMovement code too )

summer patio
#

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?

dense locust
#

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.

summer patio
#

so "is paused" is another way of saying "Player.enabled=false"

#

just shorter

dense locust
#

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

summer patio
#

can i ask what a bool dose

dense locust
#

a bool is just true or false,

summer patio
#

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

dense locust
# summer patio 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).

#

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#?

▶ Play video