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
1
Question by bromley · May 27, 2016 at 05:08 PM · gameobjectarrayforeachorder

Order a GameObject Array

i want to order a GameObject Array called with the GameObject.FindObjectsWithTag

i'm using this part of script:

     itemsCount = items.Length;
         itemsIdentity = new int[itemsCount];
 
         for (int i = 1; i <= itemsCount; i++) 
         {
             foreach (GameObject item in items) 
             {
                 itemScript = item.GetComponent <ItemData> ();
             
                 if (itemScript.ID == i) 
                 {
                     items.SetValue (item, itemScript.ID - 1);
                     itemsIdentity [itemScript.ID - 1] = itemScript.ID;
                 }
             }
         }

i don't understand why, but with the item.SetValue (item, itemScript.ID - 1); line, in the Array will be only one of these GameObject.

any advice?

Comment
Add comment · Show 1
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 NoseKills · May 31, 2016 at 03:41 PM 0
Share

Do you actually need them to be sorted or just need to reference them by ID? You could use a Dictionary < int, GameObject >

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by RavenOfCode · May 28, 2016 at 04:21 AM

Take out the for loop its causing your itemScript.ID to get messed up (ie. the last run of the foreach loop they will all have the same ID.

Comment
Add comment · Show 3 · 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 bromley · May 28, 2016 at 06:02 AM 0
Share

I understand the problem: if i set a different value of the array during the foreach loop, this one doesn't read the object who changed the value, so i get only one object for all the array.

Now the question is another one: how can I order my Array?

avatar image RavenOfCode · May 31, 2016 at 03:02 PM 0
Share

Sorry for the late response, I didn't get a notification.

Try using lists ins$$anonymous$$d.

Something like this:

 public int size; //the size you want the list to be
 private List<GameObject> items; //default size is 0
 
 void Start ()
 {
     int amount;
     while (amount < size) //keeps searching while items isn't full
                 {
                     items.Insert(amount, GameObject.FindGameObjectsWithTag("items")[amount]); //adds an item from FindGameObjectsWithTag to items
                     amount++; //increases amount towards size
                 }
 }
avatar image bromley RavenOfCode · May 31, 2016 at 04:03 PM 0
Share

thanks for the help, this can be useful

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

53 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

Related Questions

Sorting an Array of GameObjects by Name 1 Answer

How would I find the name of all game objects in a c# List 1 Answer

Problem with foreach loop with GameObject array 2 Answers

Populate an array of GameObjects with collision? 1 Answer

HELP Find gameObject With tag in another array 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