Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 JimmyCushnie · Aug 08, 2017 at 05:27 PM · fixedupdateupdate function

How can I make it so that a different set of code is called in Update() depending on a variable determined in the Start() function?

Right now, my code looks something like this:

 private int i;
 
 Start()
 {
 // determine if i should be 0, 1, 2, or 3
 }
 
 Update()
 { 
 if ( i == 0)
 {
 // do something
 }
 if(i == 1)
 {
 // do something else
 }
 if(i == 2)
 {
 // do something else
 }
 if(i == 3)
 {
 // do something else
 }
 }

Obviously, this is bad for performance; since the value of i will never change, 3/4 of the highest-level checks in the update function are useless at any given time. How can I make it so that the start function determines which code is run during Update()?

Comment
Add comment · Show 4
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 ShadyProductions · Aug 08, 2017 at 05:42 PM 0
Share

How else would you do it?

avatar image JimmyCushnie ShadyProductions · Aug 08, 2017 at 05:49 PM 0
Share

What I want is the equivalent of four Update() functions, only one of which runs for the duration of the script. Checking the value of i every frame is wasteful because it will never change.

avatar image ShadyProductions JimmyCushnie · Aug 08, 2017 at 05:55 PM 0
Share

Then do it all in start?

Show more comments

2 Replies

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

Answer by LilGames · Aug 08, 2017 at 05:50 PM

There is absolutely nothing wrong with doing those IF tests, except that you should be using ELSE to prevent unecessary condition tests of i once a true condition is met. For example I've modified your code here:

      Update()
      { 
         if ( i == 0)
         {
             // do something
         }
         else if(i == 1)
         {
             // do something else
         }
         else if(i == 2)
         {
             // do something else
         }
         else if(i == 3)
         {
             // do something else
         }
      }

You can further optimize by making the order of your IF tests based on which value is most likely to be true. Then it will run through less IFs. (But really, the performance impact is negligible unless you have hundreds)

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 JimmyCushnie · Aug 08, 2017 at 06:07 PM 0
Share

Thank you very much. I'm going with this answer because the other one is hard and I'm bad at program$$anonymous$$g :)

(But really, the performance impact is negligible unless you have hundreds)

Good to know. Thanks for answering.

avatar image
1

Answer by Senuska · Aug 08, 2017 at 06:01 PM

I would declare an enum before Start(). Like GameState:

 public enum GameState {
       Loading,
       Playing,
       Paused,
       None
   };

You can then create a state variable:

 GameState state = GameState.None;

You can set it to whatever enum value you want in start.

 void Start() {
     state = GameState.Loading;
 }

After that you have a switch statement in Update that will do different things based off of which GameState state is set to.

 void Update() {
     switch(state) {
         case GameState.Loading:
             // do loading things
         break;
         case GameState.Playing:
             // do playing things
         break;
         case GameState.Paused:
             // do paused things
         break;
         case GameState.None:
             // do nothings
         break;
         default:
             // default is a catch-all in case the state is set to something weird
         break;
     }
 }

From there you need to come up with conditions as to what will cause the state variable to be changed in the different steps (user presses the pause button while it is in the "Playing" state, so we change state to "Paused").

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 JimmyCushnie · Aug 08, 2017 at 06:07 PM 0
Share

Thank you for answering but I'm going to use the simple solution for now.

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

69 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

Related Questions

Smooth Movement Along Waypoints & Update() vs FixedUpdate() 2 Answers

Input and Rigidbody (Update and FixedUpdate) 0 Answers

Not registering input when checking input in Update and using input in FixedUpdate 1 Answer

Raycast and Physics(Ragdolls): Update() or FixedUpdate()? 1 Answer

Different game speed 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