i have a pause script that has a bool ispause and i have a button that disnable the bool but itsnt working
here’s the script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mapUIScript : MonoBehaviour {
playerMouseLook playerMouseLook;
playerMovement playerMovement;
public GameObject backButton;
public GameObject startButton;
public bool ispause;
void Start () {
playerMouseLook = GameObject.Find("player").GetComponent<playerMouseLook>();
playerMovement = GameObject.Find("player").GetComponent<playerMovement>();
playerMovement.enabled = false;
playerMouseLook.enabled = false;
Time.timeScale = 0;
ispause = true;
backButton = GameObject.Find("backButton");
startButton = GameObject.Find("playButton");
}
void Update () {
pause();
}
public void StartButton() // button doent work
{
ispause = false;
}
void pause()
{
if (Input.GetButtonDown("pause")) // pause = esc key and works fine
{
ispause = !ispause;
}
if (ispause)
{
Time.timeScale = 0;
playerMovement.enabled = false;
playerMouseLook.enabled = false;
backButton.SetActive(true);
startButton.SetActive(true);
}
if(!ispause)
{
Time.timeScale = 1;
playerMovement.enabled = true;
playerMouseLook.enabled = true;
backButton.SetActive(false);
startButton.SetActive(false);
}
}
}
Update doesn’t run when the timescale is 0 you need to manually call Pause() inside the StartButton().
Also remove those gameobject.Find() in the pause function, you already found the scripts in Start, its just wasted performance in pause. theres more that I would do to clean up this script but for now that should get it working
got an error:
UnassignedReferenceException: The variable backButton of mapUIScript has not been assigned.
You probably need to assign the backButton variable of the mapUIScript script in the inspector.
mapUIScript.pause () (at Assets/scripts/mapUIScript.cs:58)
mapUIScript.Update () (at Assets/scripts/mapUIScript.cs:24)
mapUIScript.StartButton () (at Assets/scripts/mapUIScript.cs:28)
UnityEngine.Events.InvokableCall.Invoke (System.Object[ ] args) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:153)
UnityEngine.Events.InvokableCallList.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:630)
UnityEngine.Events.UnityEventBase.Invoke (System.Object[ ] parameters) (at C:/buildslave/unity/build/Runtime/Export/UnityEvent.cs:765)
UnityEngine.Events.UnityEvent.Invoke () (at C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:53)
UnityEngine.UI.Button.Press () (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:35)
UnityEngine.UI.Button.OnPointerClick (UnityEngine.EventSystems.PointerEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/UI/Core/Button.cs:44)
UnityEngine.EventSystems.ExecuteEvents.Execute (IPointerClickHandler handler, UnityEngine.EventSystems.BaseEventData eventData) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:52)
UnityEngine.EventSystems.ExecuteEvents.Execute[IPointerClickHandler] (UnityEngine.GameObject target, UnityEngine.EventSystems.BaseEventData eventData, UnityEngine.EventSystems.EventFunction`1 functor) (at C:/buildslave/unity/build/Extensions/guisystem/UnityEngine.UI/EventSystem/ExecuteEvents.cs:269)
UnityEngine.EventSystems.EventSystem:Update()
but it automaticly assaign the stuff in the code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class mapUIScript : MonoBehaviour {
public playerMouseLook playerMouseLook;
public playerMovement playerMovement;
public bool ispause;
public GameObject backButton;
public GameObject startButton;
void Start () {
playerMouseLook = GameObject.Find("player").GetComponent<playerMouseLook>();
playerMovement = GameObject.Find("player").GetComponent<playerMovement>();
playerMovement.enabled = false;
playerMouseLook.enabled = false;
Time.timeScale = 0;
ispause = true;
backButton = GameObject.Find("backButton");
startButton = GameObject.Find("playButton");
}
void Update () {
pause();
}
public void StartButton() // button doent work
{
Update();
pause();
ispause = false;
}
public void pause()
{
if (Input.GetButtonDown("pause")) // pause = esc key and works fine
{
ispause = !ispause;
}
if (ispause)
{
playerMouseLook = GameObject.Find("player").GetComponent<playerMouseLook>();
playerMovement = GameObject.Find("player").GetComponent<playerMovement>();
Time.timeScale = 0;
playerMovement.enabled = false;
playerMouseLook.enabled = false;
backButton.SetActive(true);
startButton.SetActive(true);
}
if(!ispause)
{
playerMouseLook = GameObject.Find("player").GetComponent<playerMouseLook>();
playerMovement = GameObject.Find("player").GetComponent<playerMovement>();
Time.timeScale = 1;
playerMovement.enabled = true;
playerMouseLook.enabled = true;
backButton.SetActive(false);
startButton.SetActive(false);
}
}
}
Make sure you have an object named backButton that is Active in your scene. GameObject.Find doesn’t work on inactive objects.
Since you’re making the variable public, you are probably better off just dragging and dropping the backButton into the variable in the inspector.
i did that but still doent work
Did you leave backButton = GameObject.Find in your start function?
Are you getting the same error?
yea but when i press the button(activate the startButton() ) then it shows an error
If you leave the backButton = GameObject.find in your start, it will set backButton to null I’m pretty sure, since it can’t find the button, which will give you the same error.
Also note that Update is a built in unity method that is called every frame when the object is active. There is no need to call it in StartButton like that.
found it, i had a prefab for the button but that uses an old script, thanks for the help tho
but nothing happend when i press the button…lol