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
1
Question by Qasem2014 · Jan 08, 2015 at 12:40 PM · loopfor

crazy loop !!! HELP ME

hi

i have a loop :

 for(i=32;i>=0;i--)
 {
     if(i%8 == 1) { StartPositionX = -40; }
     if(i<8)
     { StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
     else if(i>=8 && i<16)
     { StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
     else if(i>=16 && i<24)
     { StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
     else if(i>=24)
     { StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
 }

this loop print "StartPositionX" variable for me after minus it by 4 and reset it to "-40" after i%8 equals 1 .

but when last condition comes , result goes crazy !!!! here is the result(in image) :

alt text

i don't know why "StartPositionX" variable in last condition don't minus by 4 !!!!!

How can I Fix It ?

3.jpg (212.9 kB)
Comment
Add comment · Show 5
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 Scribe · Jan 08, 2015 at 12:50 PM 0
Share

they all do $$anonymous$$us by 4, except as i = 1,9,17,25... aka when i%8 = 1 what is your exact expected behaviour?

Your starting value of StartPositionX is -41.5f.

And why do have all these if statements to do the exact same thing? all you've got is, for all values of i, StartPositionX -= 4;

avatar image tanoshimi · Jan 08, 2015 at 01:52 PM 1
Share

I've a feeling you have misunderstood the meaning of this line:

 if(i%8 == 1) { StartPositionX = -40; }

What, in words, do you want that to do?

avatar image Qasem2014 · Jan 08, 2015 at 05:47 PM 0
Share

thank you for replies :)

as i killmobil say its a public variable , i make it private and problem solved :)

killmobil convert your comment to answer , then i tick it and close the question .

avatar image richyrich · Jan 08, 2015 at 06:28 PM 0
Share

@Qasem2014 - Your script still has lots of issues as tanoshimi and Scribe have commented. For example your current code:

 if(i%8 == 1) { StartPositionX = -40; }
     if(i<8)
      { StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
      else if(i>=8 && i<16)
      { StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
      else if(i>=16 && i<24)
      { StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }
      else if(i>=24)
      { StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX); }

The lines above are currently the same as writing (i.e. could be replaced by) these next two lines alone

 if(i%8 == 1) { StartPositionX = -40; }
 StartPositionX -= 4; print("i : " + i + " - X : " + StartPositionX);


Is this by design?

avatar image Qasem2014 · Jan 08, 2015 at 07:20 PM 0
Share

richyrich , its a sample of my code . and i should set "StartPositionX" variable private or change it to -40 in component window .

i have a -41 number for it before i change it to -40 . because of that my first result is different from what i expect .

1 Reply

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

Answer by KillMobil · Jan 08, 2015 at 12:50 PM

I just ran the script and look like it was fine on my sidealt text

is it possible that the Variable StartPositionX gets affected by other factors? Is it a public variable? is set on to be equal transform later in your script?


screen shot 2015-01-08 at 2.47.45 pm.png (104.4 kB)
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 Qasem2014 · Jan 08, 2015 at 07:23 PM 0
Share

i make "StartPositionX" variable private and problem solved :)

before i change my variable to -40 , i have -44 already assign to it in component window

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

28 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

Related Questions

"for" loop - DrawTexture problem. 0 Answers

For loop C#? 2 Answers

Using for as while. 3 Answers

Shorten code with FOR loop 1 Answer

[Solved]Using multiple arrays in one statement? 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