- Home /
InGame menu not working between different scenes using a prefab
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityStandardAssets.Characters.FirstPerson;
public class PauseMenu : MonoBehaviour
{
public static bool GameIsPaused = false;
public GameObject PauseMenuUI;
// Start is called before the first frame update
void Start()
{
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Escape))
{
if (GameIsPaused)
{
Resume();
} else
{
Pause();
}
}
}
public void Resume ()
{
PauseMenuUI.SetActive(false);
Time.timeScale = 1f;
GameIsPaused = false;
GameObject.Find("FPSController").GetComponent<FirstPersonController>().enabled = true;
Cursor.visible = false;
Cursor.lockState = CursorLockMode.Locked;
}
public void Pause ()
{
PauseMenuUI.SetActive(true);
Time.timeScale = 0f;
GameIsPaused = true;
GameObject.Find("FPSController").GetComponent<FirstPersonController>().enabled = false;
Cursor.visible = true;
Cursor.lockState = CursorLockMode.None;
}
public void LoadMenu()
{
SceneManager.LoadScene("MainMenu");
}
public void OptionsMenu()
{
}
}
My question is:
How come when i make a prefab of my ingame menu, the buttons no longer work between the scenes? I thought that if i just make a prefab. It should work across any scene that i put it in.
I dont think that this is a script problem, but i added what i had attached to the menu.
This menu works in the scene that i built it in, and i can also pull up the menu in any scene i drop it in, but i it is not functional. I can't figure out what i goofed on so any feedback will help!
Answer by JonPQ · Jan 16, 2019 at 05:49 PM
check the layers in each scene, make sure your Ui is under a canvas(if it needs to be) and make sure there is a graphicRaycaster component with appropriate layer mask (in both of your scenes. On parent/top level of your Ui container Object. If it works in one scene, it should work in the other as long as your setup is correct.
Only other thought, is check the buttons, see what code it is trying to execute on the button script... maybe it was trying to execute something on an object with a script that is in one scene, but not the other scene. If the script is on the same ui object you are moving, then that is not the issue.
Thanks a lot! you made me realize that I needed to copy the eventSystemHandler that goes with the UI to make it work!
Answer by badadam · Jan 16, 2019 at 05:46 PM
on click event of button need a object which is in current scene and method which is component of the object added to event. When you use UI menu as prefab you can't add the event object without extra code. I think you shouldn't UI objects as prefab. Create a UI Object in a scene, copy it to another scene and modify it. Change game object of on click event with a object in current scene.
Thanks a lot for your feedback!! I figured out that I didn't copy the eventSystemHandler over to the scene that i added the menu to. thanks a lot for your help!