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 Unity_Mike2012 · Aug 28, 2012 at 11:45 AM · variablestutoriallock

Possible to lock a variable like moveSpeed?

In the 3D Platform Tutorial there is a ThirdPersonController script that utilizes a private variable called "moveSpeed". I have made the variable just a regular var by deleting "private" from the line of code so that I can use it in the ThirdPersonStatus script. I am doing this because I would like to achieve a completely motionless character upon respawn, otherwise if the character is moving upon death he carries over the motion values when respawned. In other words, if the character is flying through the air or is running when killed, he respawns with that motion still going and then comes to a stop so long as you aren't pressing anything on your input device. So again, I was trying to manipulate the moveSpeed variable to "0" when he respawns, and I have achieved this by using these lines of code:

 var controller: ThirdPersonController;
     controller = GetComponent(ThirdPersonController);
         controller.moveSpeed = 0.0;

this is used in the function Die() of the ThirdPersonStatus script. My question goes beyond this though. I was wondering if for some reason ever that I want to lock a variable in another script, that is, keep it from changing despite what you press on your input device, how would I do that? I understand there is a "lock" operator in Javascript but I do not know how it is used. I was thinking along the lines of this:

 lock.controller.moveSpeed;
 
 //or
 
 controller.moveSpeed.lock;

but when I use either of these (not at the same time, of course) I get this error: "Expressions in statements must only be executed for their side-effects." I do not know what this means, I am still learning. I am just wondering because it would be good to know how to disable the moveSpeed temporarily, if I wanted, for what ever purpose related to the game it's used in. I've also tried using

 controller.moveSpeed.enabled = false;

 

but that doesn't seem to work either. What can I use to "lock" a variable to what ever value at any given time? Thank you for your time!

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Wiki

Answer by zyzyx · Aug 28, 2012 at 12:12 PM

I'm not sure how it is done in UnityScript.

In C# you could use a bool variable and a property to "lock" your variable.

     public bool LockMoveSpeed;
 
     private float moveSpeed;
     public float MoveSpeed
     {
         get { return value; }
         set
         {
             if (!LockMoveSpeed)
                 moveSpeed = value;
         }
     }
Comment
Add comment · Show 1 · 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 Unity_Mike2012 · Aug 28, 2012 at 01:00 PM 0
Share

I'm new to program$$anonymous$$g and game design and I've only been fooling around with Java the past week or so, but I appreciate your feedback! This is a great idea I didn't think of before, to make the value a condition. Can C#, Java and Boo work together on the same game objects, or do all the scripts on a given object have to be same i.e. all Java, or all C# etc?

avatar image
0

Answer by Dasherz · Aug 28, 2012 at 12:50 PM

why not just set movespeed to 0 upon death or reset all variables???

Comment
Add comment · Show 1 · 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 Unity_Mike2012 · Aug 28, 2012 at 12:58 PM 0
Share

yeah i know, I did that and it works great, but my question is say I want to use something that the character picks up or triggers or whatever and the speed he had when going into that trigger or upon picking that item up is then locked? So like if I pick up an item, and I'm traveling at a moveSpeed of 5.68397 or whatever, then that number gets locked until I drop that item, remove it or collide with another trigger or something. The thing is when you just reset the value to zero, you can then press forward on your keyboard or joystick and the value changes again. $$anonymous$$y thought was to have the value locked so that input devices no longer have any effect, you know? But I think "zyzyx" brought up a good point that I could just make the value a condition so that it remains locked until the condition is expired or another condition overrides it or whatever. Anyway, thanks for the feedback, very much appreciated!

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

9 People are following this question.

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

Related Questions

Is there any performance or memory usage diffrence? 0 Answers

Moving platform not taking mesh with it 0 Answers

3D Platformer Tutorial - 5 Answers

3D Platform Tutorial Problem 1 Answer

Lerpz tutorial problems 2 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