Bug with script or with Unity?

Hello,

First, I have to say that I’m new to both Unity and to Unityscript. Recently, I have been following a tutorial from Lynda.com and have been cruising along until a few hours ago. I was instructed to create a simple script used to control the cursor on the game screen. The script is supposed to freeze the game state when I press the “esc” key so that I can access the editor without manipulating the camera every time I alternate between the game screen and the editor. Pressing the “esc” key again is supposed to unfreeze the game state and so on.
The process seems to work as intended until I save both the scene and project and reload the project in Unity. Suddenly, the freeze/unfreeze process no longer works. Instead, the cursor freezes when I press the “esc” key and then stays frozen. Pressing the “esc” again has no effect. The game state stays frozen until I exit game screen and back into the editor. I’ve never posted a script before, but I’m going to try to post two scripts here and explain the process in hopes that someone can explain what is going wrong.

I have a game object in the hierarchy called, “First Person Controller” and another called, “Game Manager.” They work together to manipulate the mess I’ve created. Attached to the “First Person Controller” is this script:

private var motor : CharacterMotor;

internal var noInput : boolean;

// Use this for initialization
function Awake () {
	motor = GetComponent(CharacterMotor);
}

// Update is called once per frame
function Update () {

	if (noInput) Input.ResetInputAxes ();

	// Get the input vector from keyboard or analog stick
	var directionVector = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
	
	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");
}


function ToggleInput (state : boolean) {

	noInput = state;

}

// Require a character controller to be attached to the same game object
@script RequireComponent (CharacterMotor)
@script AddComponentMenu ("Character/FPS Input Controller")

-------------------Then, attached to the “Game Manager” is this script:----------------------

#pragma strict

var fpc : GameObject;

function Start () {

	//hide cursor
	//Screen.showCursor = false;

}

function Update () {

	if (Input.GetKeyDown ("escape")) {
	
		if (Screen.showCursor == false) {
		
		fpc.SendMessage("ToggleInput", true);
		
			Screen.showCursor = true;
		}
		
		else {
		
			fpc.SendMessage("ToggleInput", false);
		
			Screen.showCursor = false;
			
		}
		
	}

}

I’ve inserted the “First Person Controller” game object from the hierarchy to the “Fpc” variable in the inspector for the “Game Manager” script. This process is supposed to get the desired result. And, it does at first. Until I save all scripts, scenes, and projects and try to reload it all again. Then I get the mess I described. If I then un-attach all said scripts from objects and reassemble them, it all works fine again…until I save, exit, and reload again. I’ve been doing this for a few hours to try and sort it out, but to no avail. Can anyone help me here? Thanks in advance. :slight_smile:

Just realized I can attach scripts to posts. I’ll attach them as well.

1024927–38034–$GameManager.js (390 Bytes)
1024927–38035–$FPSInputController.js (1.58 KB)

Use code tags.

if (noInput) Input.ResetInputAxes ();

If noInput is true than you wipe all input. This prevents you from ever receiving the second esc key press if your fpc script gets updated before your GameManager.

Instead try this

if (noInput) 
    return;

Thanks for the reply. i tried what you suggested and it allowed me to continuously press the “esc” key, but not with the desired effect. Instead, when I first press the key, it blinks. Then when I press it again, the cursor becomes solid again and so on. The game state does not freeze at all.

Lets try debugging this with a few print statements.

function ToggleInput (state : boolean) {
    Debug.log("Changing State " + state);
    noInput = state;
}

You should be getting one print for each “esc” press.

You can also try

if (noInput) 
{
    Debug.log("LOCKED");
    return;
}

Ok, I tried with just the first example you gave me. On the first press of the “esc” key, I get, “Changing state false” and the blinking cursor. On the second press, I get, “Changing state true” and a solid cursor and so on with every press of the key.

Then I used both of your log examples at the same time and I got “changing state false” with the first press and then a really quick flash of “changing state true” then “LOCKED” with the second. Same cursor behavior. Game state never frozen.

Then I tried your debug logs with the original script lines and I got a weird error. “The referenced script on this behavior is missing.” And the line: (Filename: C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/Mono/MonoBehaviour.cpp Line: 1509) came up before I pressed the “esc” key.
When I press the key, I get “changing state false” and then on the second press, i get, “Changing state true.” Then a total game freeze until I close the game.

Does that mean anything important?

EDIT: That “C:/BuildAgent/work/d9c061b1c154f5ae/Runtime/Mono/MonoBehaviour.cpp” directory doesn’t even exist.

Edit.edit: Fortunately, the tutorial provides an updated project folder with each step. So, I loaded the supplied folder (which is apparently an exact copy of the files used in the tutorial) and this time the game state freeze part worked as intended, however instead of disappearing when I pressed “esc”, the cursor just flashed continuously instead. So, I saved and exited everything and reloaded. This time, the game state freeze worked and the cursor didn’t flash continuously, but it never did disappear. I have a feeling it’s gonna be a long, buggy road on the way to Unity fluency…

Hi Bob,

Silly question, but which version of Unity was the tutorial you’re following made for? There are some very old tutorials out there, but major releases of Unity (2.0, 3.0, 4.0) are less interested in retaining backwards compatibility with old scripting APIs, so tutorials have to be updated accordingly.

Even UT’s own “3D Platformer Tutorial” throws up a couple of errors now with Unity 3.5.5, mainly because it was written for the Unity 2.0 release and needs to be updated to remove references to obsolete APIs and methods of working. (For example, Unity 2.x didn’t support “generics”.)

Stimarco, that’s a relevant question. The tutorial was made using 3.5. It’s actually a fairly easy guide to follow. Right now, I’m thinking the error occurred due to my own inability to stick strictly to the script. Soon as I learn how something works, I seem to think I can improve it somehow. I believe that’s how my project crashed. Also, is it possible that I’m getting the occasional flashing cursor due to my cheap computer? Maybe it just can’t keep up? I don’t know, but I am looking forward to an upgrade this coming week!
I tried to start learning using some outdated YouTube videos made by the TornadoTwins. Didn’t take me long to realize how futile that was. So, I looked around and was able to find some pretty current tutorial resources. Thanks for the reply.

If you make the variable public you can assign the value in the inspector