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 BLF-Games · Jun 26, 2012 at 03:44 PM · arrayloop

Array loop first element goes last?

I'm making an array filled with some gameObjects all tagged as "Spells". What is happening is that always the first element is shown last when iterating through the array. Here is the code:

 var Pre_Spells : GameObject[];
 
 private var i : int = 0;
 private var item_pos : int = 0;
 
 function Start() {
    fillArray();
 }
 
 function OnGUI() {
    for(i=0; i<Pre_Spells.length; i++) {
       GUI.Box(Rect(5,35+item_pos,390,250), GUIContent(Pre_Spells[i].GetComponent(Spell).Spell_Name,Pre_Spells[i].GetComponent(Spell).Spell_Icon));
       if(GUI.Button(Rect(5,200+item_pos,150,50), "Check")) {
          Debug.Log(i);
       }
    item_pos = 250 * i;
    }
 }
 
 function fillArray() {
    Pre_Spells = GameObject.FindGameObjectsWithTag("Spells");
 }

   

So, for example, Pre_Spell is filled like this:

 Pre_Spell[0] = spell0
 Pre_Spell[1] = spell1
 Pre_Spell[2] = spell2

But when iterating the array it shows

 spell1
 spell2
 spell0 -> it was the first element, but shown last!

The gui.button "check" actually shows the correct array position.

Is this a normal behaviour? Am i doing something wrong? any help is much appreciated.

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 rutter · Jun 26, 2012 at 04:20 PM 1
Share

In your fillArray() function, you populate the array using GameObject.FindGameObjectsWithTag(), which will overwrite any current values. If you're depending on values from the inspector to have any particular order, could that explain your problem?

avatar image BLF-Games · Jun 26, 2012 at 04:23 PM 0
Share

It always fill in the same order. Still I tried populate it manually excluding fillArray() function... and the same problem persist. First element goes last.

1 Reply

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

Answer by BLF-Games · Jun 26, 2012 at 09:40 PM

Ok, after testing a lot, the problem wasn't in the loop, but in the item_pos variable...

So now it works perfect, the final code:

 var Pre_Spells : GameObject[];
 
 private var i : int = 0;
 private var item_pos : int = 0;
 
 function Start() {
    fillArray();
 }
 
 function OnGUI() {
    for(i=0; i<Pre_Spells.length; i++) {
       item_pos = 250 * i; //the right place for this
       GUI.Box(Rect(5,35+item_pos,390,250), GUIContent(Pre_Spells[i].GetComponent(Spell).Spell_Name,Pre_Spells[i].GetComponent(Spell).Spell_Icon));
       if(GUI.Button(Rect(5,200+item_pos,150,50), "Check")) {
          Debug.Log(i);
       }
       //item_pos = 250 * i; was here before and made first element appear last
    }
 }
 
 function fillArray() {
    Pre_Spells = GameObject.FindGameObjectsWithTag("Spells");
 }

I'm so sorry I opened a question here and discover my stupid error... thanks anyway for everyone who read this or those who tried to help.

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 BLF-Games · Jun 27, 2012 at 03:40 PM 0
Share

I'm leaving this "issue" in case anyone has the same problem and the same burned neurons to solve it xD Should i delete it?

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Can someone give me an example on looping through arrays? 0 Answers

Creating a looping spawn with an Array 0 Answers

Saving and applying an array of Vector3 velocities on an array of GameObjects 1 Answer

creating a 2d array for grid (Not been asked i swear) 1 Answer

Changing state on touch, then looping back to beginning,Returning to initial state after game objects changed (or possibly pulled from array) 0 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