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 Klasmic · Jul 24, 2017 at 05:26 AM · for-loopfor loopfor

forloop running more than said amout of times

i am trying to run a bit of code once, i cant do this at start unfortunately so i tried a for loop, but it did the code multiple times i debug the temporary variable i and it stayed at 0 the whole time so i added the line

 i = i + 1

this seemed to add 1 to i but it still ran the code multiple times. I know its not the moveToPos method as when i binded it to a key it worked fine. heres my code:

     void Update () {
         for (int i = 0; i < 1; i++) {
             moveToPos ();
         }
     }

 public void moveToPos(){
         startpos = new Vector3 (transform.position.x, transform.position.y - 10);
         RaycastHit2D hit2d = Physics2D.Raycast (startpos,Vector2.down);
 
         if (hit2d.collider != null) {
             gameObject.transform.position = new Vector3 (gameObject.transform.position.x, 
                 hit2d.collider.gameObject.transform.position.y);
         }
     }

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Kishotta · Jul 24, 2017 at 05:33 AM

Do you mean to call moveToPos () every frame? If you only want it run once, you should move the call from Update () to either Awake () or Start ().

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 Klasmic · Jul 24, 2017 at 05:44 AM 0
Share

@$$anonymous$$ishotta I cant do it at start

avatar image Kishotta Klasmic · Jul 24, 2017 at 05:49 AM 0
Share

Then you'll need to wrap your for loop in a conditional check so that it only fires when you want it to (and the for loop is not actually needed here):

 void Update () {
     if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.Space)) { // As an example
         moveToPos ();
     }
 }

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

Glitch in Monodevelop for loops!!! 1 Answer

Finding nearest object with a certain tag without for loops. 1 Answer

List of Transforms updating all items when adding variable. 1 Answer

Having Trouble with this For Loop 1 Answer

OnCollisionEnter for loop returning error 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