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
0
Question by MiraiTunga · Jul 07, 2013 at 06:23 PM · arraysstartfor loopforloop

For loop not running correctly inside start ()

I Am trying to run through an array of game objects and make the first item be the "leader" then for the rest of the items in the array assign different positions depending on the index value the else part of the for loop is not executing i also tried use another if and an else if that contain (2%!=0) but still no luck`void Start () {

     GameObject[] fighters;
     fighters = GameObject.FindGameObjectsWithTag ("Fighter");
     
     //set leader trasform to nothing  on first item in arry witch is the leader
     fighters [0].GetComponent<formation> ().leader = null;
     
     for (int i=0; i < fighters.Length; i++) {
         
         //set the item on indexzero to be the leader of all other items in arry
         //the +1 is to skip the first elment which is the leader
         fighters [i + 1].GetComponent<formation> ().leader = fighters [0].transform;
         
         //assighn items dpending on index number to diffrent postion each time the for loop runs
         if (i % 2 == 0) {
         
             fighters [i].GetComponent<formation> ().setSide = setSide;
             setSide = setSide + 7;
         } else {
             fighters [i].GetComponent<formation> ().setSide = setSide;
             setSide = setSide - 7;
         }
     }

 }`

been working at it for hours dont know what i'm doing wrong

Comment
Add comment · Show 2
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 oliverblitz · Jul 07, 2013 at 06:32 PM 1
Share

If the else part is not being executed, it could be that your fighters array is only one element long. You could use Debug.Log to print out and verify the length of fighers.

One other thing that should crash your loop is that you let your i run up to Length-1, but then access fighers[ i + 1 ], which would be fighers[ Length ] and is hence out of bounds of your array.

avatar image MiraiTunga · Jul 08, 2013 at 08:00 AM 0
Share

i fixed it so its length+1 but still getting the same problem the fighters assigned -7 just all equal to 0 ins$$anonymous$$d of -7 -14 - 21 etc this are the results i am expecting on each item in the array for its setback value 0,7,-7,14,-14,21 ,-21 etc but i am getting 0,7,0,7,0

1 Reply

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

Answer by Nanity · Jul 08, 2013 at 08:22 AM

Still not sure what you want to achieve with your code, but from your description of the numbers I can see the issue, in one cycle you are adding +7 in the next you add -7, so you're at the beginning (0).

Correct code:

 if
 (i % 2 == 0) {
     fighters [i].GetComponent<formation> ().setSide = -setSide;
     setSide += 7;
 } else {
     fighters [i].GetComponent<formation> ().setSide = setSide;
 }
 
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 MiraiTunga · Jul 08, 2013 at 01:04 PM 1
Share

thanks a bunch it seems so obvious now thanks heaps !! i had to move the setSide += 7; out side of the if tho and start my i=1 and it did what i wanted it to

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Problem with array on gameobjects 2 Answers

How can I code a pattern detector in an array of integers? 1 Answer

I have never seen a null reference exception like this one! 1 Answer

NullReference when accessing GameObject in array (C#) 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