InvalidCastException: Cannot cast from source type to destination type

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#.

You need to write “[ code = javascript ]”, or select Javascript from the drop-down list when pasting code with the code button. Anyway, you misplaced the closing parenthesis with Input.GetButton(“Cancel”.

–Eric

Alright, thanks.

I noticed that. However, when I put the closing parenthesis in (so it looks like this:

if(Input.GetButton(“Cancel”) && Time.timeScale == 0){

), it doesn’t work either. Doesn’t show the pause screen at all.

I’ve been playing around with it. I kind of followed this (http://unity3d.com/learn/tutorials/modules/beginner/scripting/if-statements) and put the two if statements in their own function, which I called “Pause.” I then put them together with “if” and “else if,” which I think makes it mutually exclusive. The code is below.

function Update () {

if(Input.GetButton("Cancel"))
        Pause();

then after Update:

function Pause()
{
    //For initial pause action
    if(Time.timeScale == 1){
        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;
    }
  
    else if(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;
    }
}

It works now. However, it moves too fast–in a “normal” key press you’ll go from paused to playing again. And if you hold Escape you can see it flicker back and forth.

So what I need now is a way to restrict the input, or restrict how fast it can return the function.

Edit: aaaaand, I changed the “GetButton” to “GetKeyUp(KeyCode.Escape).” So it only returns when Escape is released.

it’s all working now.