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
1
Question by edve98 · Mar 12, 2014 at 04:35 PM · javascriptenumstatic variable

How to define wich static variable to use with enum?

Here's the deal: I need one script to use one of the static variables in another script. This how the first script looks like (ButtonThatBindsKeys.js) :

 //Button activeness variables
 
 var GotKeyDown : boolean;
 
 var ActivatonColor : Color;
 var DeActivationColor : Color;
 
 
 //For the WhathToBind dropdown
 
 enum MyEnum{
 
 Forward,
 Backwards,
 Left,
 Right,
 Interact,
 ToggleFlashlight,
 Sprint
 
 }
 
 
 //Whath this script shuold bind?
 
 var WhatToBind = MyEnum.Forward;
 
 
 
 function OnMouseDown () {
     
     if(BindingGuard.AlreadyActive == false){
     
     //Makes sure other button won't be activated
     
     BindingGuard.AlreadyActive = true;
     
     
     //Makes button active
     
     renderer.material.color = ActivatonColor;
     
     GotKeyDown = false;
     
     
     
     
     
     while (GotKeyDown == false) {
         
         
         
         if(BindingGuard.e.isKey && BindingGuard.e.keyCode != KeyCode.None){
                 
                 //Here is the actuall binding
                 
                 
                 Settings.WhatToBind = BindingGuard.e.keyCode;
                 
                 
                 
                 
                 
                 //De-activates the button
                 
                 renderer.material.color = DeActivationColor;
                 
                 GotKeyDown = true;
                 
                 //Makes sure that other button can be activated
                 
                 BindingGuard.AlreadyActive = false;
                 
             }
             //So the game dosen't freeze
             yield;
         }
     }
     else{
         
         print("Another or this one key binding button is already active :(");
         
     }
     
 }

The second script only needed for this script to work properly (BindingGuard.js) :

 static var AlreadyActive : boolean = false;
 
 static var e : Event;
 
 
 function OnGUI() {
 
     e = Event.current;
     
 }

And the third script (only part of it, the one that has the static variables) (Settings.js):

 static var Forward : KeyCode = KeyCode.W;
 static var Backwards : KeyCode = KeyCode.S;
 static var Left : KeyCode = KeyCode.A;
 static var Right : KeyCode = KeyCode.D;
 static var Interact : KeyCode = KeyCode.E;
 static var ToggleFlashlight : KeyCode = KeyCode.F;
 static var Sprint : KeyCode = KeyCode.LeftShift;


Edit: I posted my original code here, so it would be easier to understand what I am trying to achieve

Comment
Add comment · Show 3
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 pazzesco · Mar 12, 2014 at 05:28 PM 0
Share

On the line...

 Settings.WhatToBind = BindingGuard.e.keyCode;

I'm assu$$anonymous$$g this is abridged, and that it'd be something like Settings.Sprint = BindingGuard.e.keyCode, for example... yeah?

avatar image edve98 · Mar 12, 2014 at 05:39 PM 0
Share

Yes, that is right

avatar image pazzesco · Mar 12, 2014 at 06:48 PM 0
Share

I've updated my answer below. I think it may be more what you're looking for. You can configure a key binding through the enumeration displayed in the component's settings now and have it correctly set the static variables in the Settings script.

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by pazzesco · Mar 12, 2014 at 04:45 PM

If you're looking to set a certain static variable based on an enumeration value from one of your other scripts, you can do it with a switch statement. In your script, it looks like you have the given line as a placeholder...

 Settings.WhatToBind = BindingGuard.e.keyCode;

Using a switch statement, you can add in conditionals to set the correct static variable based on a given enumeration. Replace the placeholder with the following...

 switch (WhatToBind) { 
 
     case MyEnum.Forward:    Settings.Forward = BindingGuard.e.keyCode; break;
     case MyEnum.Backwards:    Settings.Backwards = BindingGuard.e.keyCode; break;
     case MyEnum.Left:        Settings.Left = BindingGuard.e.keyCode; break;
     case MyEnum.Right:        Settings.Right = BindingGuard.e.keyCode; break;
     case MyEnum.Interact:    Settings.Interact = BindingGuard.e.keyCode; break;
     case MyEnum.ToggleFlashlight: Settings.ToggleFlashlight = BindingGuard.e.keyCode; break;
     case MyEnum.Sprint:        Settings.Sprint = BindingGuard.e.keyCode; break;
     default: Debug.LogWarning("Unknown key binding");
 
 }

Using this method, it's the simplest fix that I know of for this problem. There are probably more effective and efficient ways of doing this. Every time you add a new key binding, you're going to have to update this block of code. At any rate, try that and let me know how it goes.

Comment
Add comment · Show 7 · 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 edve98 · Mar 12, 2014 at 04:49 PM 0
Share

Well, I have to access those static variables in other script, so your answer doesn't help me :(

avatar image pazzesco · Mar 12, 2014 at 04:54 PM 0
Share

What's the name of your other script?

avatar image edve98 · Mar 12, 2014 at 04:58 PM 0
Share

So, in these examples the scripts are named Scrip1 and Script2, just the first ones name doesn't appear anywhere.

avatar image pazzesco · Mar 12, 2014 at 05:00 PM 0
Share

Which script's variable are you trying to set, and what value are you trying to set it to?

avatar image edve98 · Mar 12, 2014 at 05:05 PM 0
Share

I'm trying to change Scrip2 variable x(depending what I choose in the DropDown variable). There is no value in this example(value of abcR), but in original script it would be keycode. BTW, I just found a little mistake in the first script

Show more comments

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

21 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a List for Inventory System, Got an error in finding constructors and variables 1 Answer

EnumStates 1 Answer

Directly accessing enum types from other scripts (javascript) 0 Answers

Convert String into KeyCode (UnityScript) 0 Answers

Need help with enum and stuff (right, left click) in JavaScript 0 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