Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Coreyf716 · Oct 18, 2012 at 01:05 AM · javascriptvariablebooleanstatic

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?

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jessespike · Oct 18, 2012 at 01:14 AM 0
Share

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?

avatar image Coreyf716 · Oct 18, 2012 at 01:17 AM 0
Share

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.

avatar image Fattie · Oct 18, 2012 at 07:13 AM 0
Share

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

avatar image Coreyf716 · Oct 18, 2012 at 10:46 PM 0
Share

Doesn't GetComponent require the component be attached to the parent of the script?

avatar image Fattie · Oct 19, 2012 at 07:07 AM 0
Share

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Coreyf716 · Oct 18, 2012 at 01:27 AM 0
Share

If 'isCrouching' is set to false, it should execute the code under '!isCrouching'.

avatar image hvilela · Oct 18, 2012 at 01:31 AM 0
Share

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.

avatar image Coreyf716 · Oct 18, 2012 at 01:34 AM 0
Share

Oh, I see where you're going with this.

avatar image hvilela · Oct 18, 2012 at 04:18 AM 0
Share

Have it solved your problem?

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

12 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges