I have a game that has a pause/menu function, tied to Cancel (or Escape), and I’m trying to write the script for unpausing the game and exiting the menu. However, I get the “InvalidCastException: Cannot cast from source type to destination type” error when I do the “unpause” code. The code is below:
(I’ve tied basically all my inputs to the FPSInputController script)
private var motor : CharacterMotor;
private var healthSlider : GameObject;
// This will return the game object named HealthSlider in the scene.
healthSlider = GameObject.Find("HealthSlider");
private var text : GameObject;
//returns the GUI text
text = GameObject.Find("Text");
private var overlay : GameObject;
// This will return the game object named Overlay in the scene.
overlay = GameObject.Find("Overlay");
//for checking if mouse is shown or not
var showingMouse : boolean;
function Awake () {
motor = GetComponent(CharacterMotor);
showingMouse = false;
Screen.showCursor = false;
Screen.lockCursor = true;
}
function Start (){
overlay.SetActive(false);
}
// Update is called once per frame
function Update () {
// Get the input vector from keyboard or analog stick
var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
//if you press RMB/L Alt and the mouse is not shown then the mouse is shown and movable then execute code
if(Input.GetButton("Fire2") && showingMouse == false){
showingMouse = true;
Screen.showCursor = true;
Screen.lockCursor = false;
}
//if you press "r" and the mouse is shown then make it not shown
else if(Input.GetButton("Fire2") && showingMouse == true){
showingMouse = false;
Screen.showCursor = false;
Screen.lockCursor = true;
}
if(Input.GetButton("Cancel")){
showingMouse = true;
Screen.showCursor = true;
Screen.lockCursor = false;
}
//Gui input
if(Input.GetButton("Cancel")){
GameObject.Find("First Person Controller").GetComponent("MouseLook").enabled = false;
GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = false;
Time.timeScale = 0;
healthSlider.SetActive(false);
text.SetActive(false);
overlay.SetActive(true);
showingMouse = true;
Screen.showCursor = true;
Screen.lockCursor = false;
}
if(Input.GetButton("Cancel" && Time.timeScale == 0)){
GameObject.Find("First Person Controller").GetComponent("MouseLook").enabled = true;
GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = true;
Time.timeScale = 1;
healthSlider.SetActive(true);
text.SetActive(true);
overlay.SetActive(false);
showingMouse = false;
Screen.showCursor = false;
Screen.lockCursor = true;
}
if (directionVector != Vector3.zero) {
// Get the length of the directon vector and then normalize it
// Dividing by the length is cheaper than normalizing when we already have the length anyway
var directionLength = directionVector.magnitude;
directionVector = directionVector / directionLength;
// Make sure the length is no bigger than 1
directionLength = Mathf.Min(1, directionLength);
// Make the input vector more sensitive towards the extremes and less sensitive in the middle
// This makes it easier to control slow speeds when using analog sticks
directionLength = directionLength * directionLength;
// Multiply the normalized direction vector by the modified length
directionVector = directionVector * directionLength;
}
// Apply the direction to the CharacterMotor
motor.inputMoveDirection = transform.rotation * directionVector;
motor.inputJump = Input.GetButton("Jump");
}
// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")
The problematic part seems to be line 68, or the second paragraph of the following code:
if(Input.GetButton("Cancel")){
GameObject.Find("First Person Controller").GetComponent("MouseLook").enabled = false;
GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = false;
Time.timeScale = 0;
healthSlider.SetActive(false);
text.SetActive(false);
overlay.SetActive(true);
showingMouse = true;
Screen.showCursor = true;
Screen.lockCursor = false;
}
if(Input.GetButton("Cancel" && Time.timeScale == 0)){
GameObject.Find("First Person Controller").GetComponent("MouseLook").enabled = true;
GameObject.Find("Main Camera").GetComponent("MouseLook").enabled = true;
Time.timeScale = 1;
healthSlider.SetActive(true);
text.SetActive(true);
overlay.SetActive(false);
showingMouse = false;
Screen.showCursor = false;
Screen.lockCursor = true;
}
For some reason Unity doesn’t like the second half of the “if” conditions there. I’ve tried basing it on a different variable, I’ve tried creating a variable that’s based on Time.timeScale…it doesn’t work.
If that second paragraph isn’t there, I can pause the game just fine (though of course I can’t exit). If the second part of the if condition isn’t there, the game continues when I press escape though the pointer appears (I suspect because the program is running both pieces of code immediately, though I’m not sure).
If it’s all there, I can’t move (though I can use the mouse to rotate the camera/spin the first-person prefab) though the rest of the scene runs fine. And I get that error.
The fact that I can’t move makes me wonder if it’s related to the character motor, but I’m not sure exactly how it would be. I looked through the character motor (which I have not edited at all), and I see references to Time.time and Time.deltaTime, but I don’t understand how adjusting the timescale would throw up an error.
Does anyone have any idea what it might be? I’m using Unity 4.6.1f1 if that makes any difference.
Edit: the code is in Javascript, I’m not sure why the code box says C#.