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 /
This question was closed Jan 21, 2019 at 08:38 PM by Patton7 for the following reason:

Other

avatar image
0
Question by Patton7 · Jan 21, 2019 at 03:49 AM · variableupdatevariablesfor-loopupdate function

Variable goes very high in update method

Hello all. So basically I'm working on a thing and based of a score variable, I want my speed variable to increase. However, the variable goes very high in the for loop. I assume this is because it is in the update method which checks it very quickly so it goes high up. Is there a way to make not have this happen? Here is the code:

   void Update()
   {
     int[] levelNums = { 5, 10, 20, 30, 40, 50 };


     for (int i = 0; i < 6; i++)
     {
         if (score == levelNums[i])
         {
             speed+= 5;
         }
     }

~Thanks in advance.

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 sean244 · Jan 21, 2019 at 04:55 AM 1
Share

If the speed is increasing too fast, why not just increment it by a smaller amount?

avatar image toddisarockstar · Jan 21, 2019 at 04:59 AM 0
Share

it is not clear what you want to do. do you want the speed to increase slowly over a fixed time? do you want the speed to match the level number?

avatar image Patton7 toddisarockstar · Jan 21, 2019 at 05:03 AM 0
Share

no so if my score is equaled to any of the values in my levelNums array, increase the speed by 5. Now the issue is if score is equaled to levelNums[i], then speed goes up to much because update is checked very frequently.

avatar image toddisarockstar Patton7 · Jan 21, 2019 at 05:17 AM 0
Share
   public float timer = 0; 
 void Update()
    {
 float seconds = 1;
     timer += Time.deltaTime;
     if(timer>seconds){timer-=seconds;
         {
 
 //now this happens once per second
      int[] levelNums = { 5, 10, 20, 30, 40, 50 };
      for (int i = 0; i < 6; i++)
      {
          if (score == levelNums[i])
          {
              speed+= 5;
          }
      }}

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by king_opping · Jan 21, 2019 at 05:20 AM

i think you need to add 1 more varible to stop speed for incrementing,

 public bool leveled;
  
 void Update()
    {
      int[] levelNums = { 5, 10, 20, 30, 40, 50 };
      for (int i = 0; i < 6; i++)
      {
          if (score == levelNums[i] && leveled==false)
          {
 if(leveled==true){leveled=false;}
              else{speed+= 5;
 leveled = true;}
          }
      }
Comment
Add comment · Show 2 · 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 sean244 · Jan 21, 2019 at 05:56 AM 0
Share

These two lines of code don't make any sense

  if (score == levelNums[i] && leveled==false)
       if(leveled==true){leveled=false;}

The second line of code can never happen. It's basically dead code and you can delete it.

The whole thing can be simplified to

 public bool leveled;

 void Update()
 {
     int[] levelNums = { 5, 10, 20, 30, 40, 50 };
     for (int i = 0; i < levelNums.Length; i++)
     {
         if (score == levelNums[i] && !leveled)
         {
             speed += 5;
             leveled = true;
         }
     }
 }
avatar image king_opping sean244 · Jan 21, 2019 at 06:22 AM 1
Share

yes, thanks mate. i just recognize it. change that to your need, i cant figure it out right now,

Follow this Question

Answers Answers and Comments

110 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

Related Questions

variable not updating inside void Update() 0 Answers

Switch statement not running on every frame 1 Answer

Show message when score achieved 1 Answer

(C#) For loop variable value not changing 0 Answers

GUI player menu is not updating values (score, exp, HPs) 1 Answer


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