Pause Menu C# Script error CS1519: Unexpected symbol in class, struct, or interface member declaration
I'm having some trouble trying to get my script working for my pause menu after following this half hour long tutorial: https://www.youtube.com/watch?v=dvyiQLkaMcg and this isn't the first time I've encountered issues with a script after following online tutorials. I've followed it to exactly match their script but I think there's something I might be missing, here's the scrip in case someone can spot where I've gone wrong:
using UnityEngine; using UnityEngine.UI; using System.Collections;
public class UIManager : MonoBehaviour {
public class GameObject PauseMenu;
public bool isPaused;
public object PauseMenu { get; private set; }
// Use this for initialization
void Start()
{
isPaused = false;
}
// Update is called once per frame
void Update()
{
if (isPaused)
{
PauseGame(true);
}
else
{
PauseGame(false);
}
if (Input.GetButtonDown("Cancel"))
{
SwitchPause();
}
}
void PauseGame(bool state)
{
if (state)
{
PauseMenu.SetActive(true);
Time.timeScale = 0.0f; //This pauses the Game
}
else
{
Time.timeScale = 1.0f; //This unpauses the Game
}
PauseMenu.SetActive(false);
}
public void SwitchPause()
{
if (isPaused)
{
isPaused = false; //Changes the value
}
else
{
isPaused = true;
}
}
}
I would really appreciate help on this because after spending hours searching and checking my script I still can't figure out what's wrong with it P:
Answer by IronarmGames_LLC · Jul 23, 2016 at 11:24 AM
@CraftyMaelyss ,it seems you are defining a class incorrectly as such in your public class GameObject PauseMenu
line.
To define a class for c#
as a Monobehaviour
in Unity, normally you would want to do it as such:
using UnityEngine;
using System.Collections;
public class DoSomethingClass : Monobehaviour {
//Put some methods, variables, functions, etc inside the class
}
Below is a simple script i whipped up that may be more what you are looking for.
using UnityEngine;
using System.Collections;
public class PauseGameTest : MonoBehaviour {
public bool isPaused;
public GameObject pauseMenu;
public KeyCode pauseGameKey;
void Start() {
isPaused = false;
}
void Update() {
if (Input.GetKeyDown (pauseGameKey)) {
if (isPaused) {
UnPauseGame ();
} else {
PauseGame ();
}
}
}
void PauseGame() {
isPaused = true;
pauseMenu.SetActive (true);
Time.timeScale = 0;
}
void UnPauseGame() {
isPaused = false;
pauseMenu.SetActive (false);
Time.timeScale = 1;
}
}
It can be done more efficient with less functions and added lines of code but i think this will better illustrate the idea to you.
Let me know if you have any questions about it.
I just tried it and it didn't work :( A pop up message appeared:
"Can't add script component 'UI$$anonymous$$anager' because the script class cannot be found. $$anonymous$$ake sure that there are no compile errors and that the file name and class name match."
This seems to happen whenever I try to use any bools in my C# script P:
This error seems to be related to another script you have thats trying to access a script named$$anonymous$$ake sure that there are no compile errors and that the file name and class name match.
UI$$anonymous$$anager
in order to add it to an object. So your issue is with another script in your project, not this one ive created. Create a new scene real quick to test on and drag the script here to an empty gameobject and press Play. Be sure to assign the variables that are in the inspector so you can test properly. Let me know what happens.
@IronarmGames_LLC I just tried it and I'm getting the exact same error as before P:
I just tested this again in an empty scene and it works flawlessly. I edited the code above for a simple fix, past that in again.
If there are any errors we'd need to see the conflicting scripts as i dont know what script would be causing that.
This isn't the first time I've experienced issues whenever I've used bools in my script, have a look at this:
http://answers.unity3d.com/questions/1216909/my-game-script-is-fine-but-im-still-getting-unexpe.html
I actually found this tutorial that actually made this work for me: https://www.sitepoint.com/adding-pause-main-menu-and-game-over-screens-in-unity/