Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 amonnone · Jun 17, 2017 at 12:29 PM · c#eventswitchenumcase

How to check if an enum’s case has changed

I am using an enum with several cases, controlled by a switch statement. Inside of Update(), I have a function running that checks the case of the enum. Therefore, I have scripts being executed every frame that relate to the current case of the enum. This doesn’t seem to be a problem, but I am running into a challenge with calculations that depend on the enum’s case.

I have a calculation that I want to run ONLY WHEN the enum’s case switches from one to another, not every frame. For example, let’s say if the enum case is “A” I want to subtract 2 from some equation in another script, while if it’s “B” I want to subtract 1 from that equation. Currently, the script is subtracting 1 every frame of the game, instead of just once. I think you see the problem here.

So I have two questions:

  • Is there a magic line of code that references when an enum’s case has changed from one thing to the next? My first stab at solving this problem was to create an Event, and at its heart this requires some code like if (enum’s case has changed) {Do this;}

  • Is there a better way to do what I’m trying to do? Is the solution to remove the script in Update() that checks the case of the enum every frame? I’m sure that what I’m trying to do has been done before, so maybe someone out there knows a better way.

For a general analogy, let’s say the player’s movement state is an enum with 3 cases: walk, crouch, prone. When it’s state is "walk", I want the player's movement speed to be multiplied by 1, "crouch" should be movement speed multiplied by 0.5, and "prone" is speed multiplied by 0.2. How would you code that so you aren’t continuously multiplying the movement speed every frame of gameplay, only upon changing from walking to crouching?

Thank you!

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by dpoly · Jun 17, 2017 at 02:02 PM

First terminology: enum is a type; an enum does not have a case, but a variable of some enum type has a value. You are asking how to write code that only executes when a variable changes value.

There are dozens of ways to do this very common thing, but the easiest by far looks like this.

 // declare variable to hold old value
 MoveKind oldmove;
 
 void Update() {
   // do this only if the value changed
   if (move != oldmove) {
     // do stuff
     oldmove = move;
   }
 }
Comment
Add comment · 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
0

Answer by ShadyProductions · Jun 17, 2017 at 12:46 PM

You can add a bool value like:

 bool alreadyDone = false;

and from the moment your enum is hit, check if your alreadyDone is false then do your logic and set alreadyDone to true;

That way it will only happen once and not repeat again.

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

Answer by XeniaPhe · Dec 06, 2021 at 11:46 PM

I know it's probably too late but what you need is a property along with the backing field which you probably already have:

 [SerializeField] float walkSpeed=1f;
 [SerializeField] float crouchSpeed=0.5f;
 [SerializeField] float proneSpeed=0.2f;
 
 float currentSpeed=1f;
 
 MovementState _currentState;
 public MovementState CurrentState
 {
     get => _currentState;
     set
     {
         _currentState=value;
         OnStateChanged(value);
     }
 }
 
 private void OnStateValueChanged(MovementState state)
 {
     switch(state)
     {
          case Walking:
          currentSpeed=walkSpeed;
               break;
          case Crouching:
          currentSpeed=crouchSpeed;
               break;
          case Proning:
          currentSpeed=proneSpeed;
               break;
     }
 }

But the problem with this is,there is nothing preventing you from using the field itself instead of the property which will then obviously not function as intended,it's ok to use the backing field for getting purposes though.

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

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

329 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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

Changing enum values 1 Answer

Adjusting location on save to avoid infinite loop. 1 Answer

I would like some help refactoring my code, I am working with switch cases concering the delivery of food items in my game 1 Answer

Error CS0135 switch enum from other class 2 Answers

What is a more efficient way to write this Switch Statement? 3 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