Other
C# - Unexpected Symbol ERROR?
So I have written this code, and I have been searching a lot of unity 3d questions, but I can't find the one I need. So basically on line 34 and 38 there seems to be an error with the "=" symbol. I am really confused on this one. (The code disables MouseLook when you press ESC and enables it with a button that says Resume Game)
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class UIManager : MonoBehaviour {
public GameObject PausePanel;
public bool isPaused;
// 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) {
PausePanel.SetActive (true);
Time.timeScale = 0.0f; //Paused
MouseLook MouseLook = GameObject.Find( "Player" ).GetComponent( "MouseLook" ) as MouseLook.MouseLook.enabled = false;
} else {
Time.timeScale = 1.0f; //Unpaused
PausePanel.SetActive (false);
MouseLook MouseLook = GameObject.Find( "Player" ).GetComponent( "MouseLook" ) as MouseLook.MouseLook.enabled = true;
}
}
public void SwitchPause () {
if (isPaused){
isPaused = false;
}
else {
isPaused = true;
}
}
}
Answer by TBruce · Apr 20, 2016 at 04:23 PM
@TacomanLuke
Without knowing what the class MouseLook looks like I can at least say that there seems to be several issues with both these statements.
"MouseLook MouseLook = " is invalid, the defined variable cannot be the same as the data type. Try MouseLook mouseLook = instead.
MouseLook.MouseLook is invalid
"MouseLook.MouseLook.enabled = true;" is invalid because you cannot implicitly convert type `bool'
Try the following instead
if ((GameObject.Find( "Player") != null) && (GameObject.Find( "Player").GetComponent( "MouseLook") != null))
{
GameObject.Find( "Player" ).GetComponent( "MouseLook" ).enabled = true;
}
or better yet if you have tagged you player as "Player" do this
if ((GameObject.FindWithTag( "Player") != null) && (GameObject.FindWithTag( "Player").GetComponent( "MouseLook") != null))
{
GameObject.FindWithTag( "Player" ).GetComponent( "MouseLook" ).enabled = true;
}