- Home /
Static Variable Problem
I have a static variable of 'isCrouching' and a script which allows the player to run:
//Run.js
var character : CharacterController = GetComponent(CharacterController);
if (!InGameMenu.isPaused) {
if (Input.GetKey(KeyCode.LeftShift)) {
Crouch.isCrouching = false;
character.SimpleMove(transform.TransformDirection (Vector3.forward) * maxSpeed);
audio.clip = runningSound;
audio.Play();
isRunning = true;
isCrouching should be disabled when the running function begins.
//Crouch.js
if (!Run.isRunning && Input.GetKeyDown(KeyCode.C)) {
isCrouching = !isCrouching;
if (isCrouching) {
playerMesh.transform.localScale = Vector3(1, 0.75, 1);
playerMesh.transform.localPosition = Vector3(0, -0.25, 0);
mainCam.transform.localPosition = Vector3(0, 0, 0);
character.height = 1.5;
character.center = Vector3(0, -0.25, 0);
}
if (!isCrouching) {
playerMesh.transform.localScale = Vector3(1, 1, 1);
playerMesh.transform.localPosition = Vector3(0, 0, 0);
mainCam.transform.localPosition = Vector3(0, 0.5, 0);
character.height = 2;
character.center = Vector3(0, 0, 0);
}
isCrouching cannot be activated while 'isRunning' is. I have no idea why, but when 'isRunning' is activated, 'isCrouching' is not deactivated. Any tips?
i don't know exactly whats wrong. but shouldn't static variables be accessed through it's class name? like ins$$anonymous$$d of typing isCrouching shouldn't it be Crouch.isCrouching?
Yes, I did that. The script on top is the running function and the one on the bottom is the crouching function. I don't believe static variables must be accessed through their class names within the script they originated from.
never ever use Static keyword for any reason.
suggest you read unitygems.com
note the beginner article on the right
urge you to read about how to use GetComponent etc !
Never use "Static".
Doesn't GetComponent require the component be attached to the parent of the script?
Hi Corey, read the BERY LONG ARTICLE on that link which beautiful explains it all. both GetComponent and Find (for other objects) are fully explained in great detail. Cheers
Answer by hvilela · Oct 18, 2012 at 01:20 AM
When the player press LeftShift your code changes the Crouch.isCrouching to false, but it do not execute the script inside "if (!isCrouching)" and won't do it cause isRunning will be true and it will never enter the "if (!Run.isRunning && Input.GetKeyDown(KeyCode.C))" statement.
So, copy all the content of "if (!isCrouching)" to "if (Input.GetKey(KeyCode.LeftShift))" so it can uncrouch when you start run.
If 'isCrouching' is set to false, it should execute the code under '!isCrouching'.
No, it shouldn't cause you also set running to true, so it will never enter the "if (!Run.isRunning && Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.C))" so "if (!isCrouching)" won't be even tested. Add Debug.Log's to your code and you'll see that the uncrouch code is never called when you hit LeftShift.
Your answer
Follow this Question
Related Questions
Trivial question for Javascript/Unityscript experts: static variables? 4 Answers
Global variables - static keyword ? UnityScript? javascript? 1 Answer
Yield Waitforseconds not working at all 3 Answers
Unknown identifier, OnCollisionEnter Error 1 Answer
An instance of type X is required to access non static member Y [javascript] 4 Answers