So I have got a script here that is meant to open a pause menu but it comes up with an error whenever I put it in my game. I am relatively new to unity though so its probably an easy fix.
Here is the script.
using UnityEngine;
using System.Collections;
using UnityStandardAssets.Characters.FirstPerson;
public class PauseGame : MonoBehaviour {
public Transform canvas;
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Escape))
{
Pause();
}
}
public void Pause()
{
if (canvas.gameObject.activeInHierarchy == false)
{
canvas.gameObject.SetActive(true);
Time.timeScale = 0;
Player.GetComponent<FirstPersonController>().enabled = false;
}
else
{
canvas.gameObject.SetActive(false);
Time.timeScale = 1;
Player.GetComponent<FirstPersonController>().enabled = true;
}
}
}
Here is a picture as well
You have to make a reference stored in a separate variable to First Person Controller and get component to it, or make this variable “static”. You can do this:
Change
Player.GetComponent (FirstPersonController) ().enabled = false;
to
GameObject.FindGameObjectWithTag (“Player”).GetComponent (FirstPersonController) ().enabled = false;
Of course change parenthesis around FirstPersonController phrase to “sharp”.
Then in hierarchy click First Person Controller and from inspector select “Player” tag.
By the way, C# isn’t good for beginners. Consider changing to JavaScript. Is much simpler.