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 /
  • Help Room /
avatar image
4
Question by slader155 · Jun 22, 2014 at 08:45 PM · arraypositiony-axis

Sorting an array based on position

Hey guys, This has been bothering me for a while now so I finally decided that it was time to ask for some help.

I'm making a game where the goal is to stack up shapes the highest possible without them falling down, and at the moment I'm working on the score system. My idea is to use the array that holds all of the shapes(Once instantiated), sort the array based the the GO's position on the Y-axis, and get the position of the last object in the array. But the problem is that I can't figure out how to sort the array.

Here is what I've got so far(not the full code, but this is the only part of the script that is relevant):

 public GameObject[] quads;
  
 void Update(){
     quads = GameObject.FindGameObjectsWithTag("quad");
 }



Edit I've also tried this:

 public GameObject[] quads;
 public GameObject[] quadPos;
      
     void Update(){
         quads = GameObject.FindGameObjectsWithTag("quad");
         quadPos = quads.OrderBy(quads => quads.transform.position.y).ToArray();
     }

But that gives me an error:

 error CS0135: `quads' conflicts with a declaration in a child block





If you guys have any ideas I would really appreciate it!

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

2 Replies

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

Answer by fafase · Jun 23, 2014 at 06:15 AM

So you can use the Sort method but here is your error.

First, you need to add

  using System.Linq;

You might have it already or the error is about to show once you fix your error.

The compiler tells you you have a conflict in a child block. The child block happens to be the inner methods run by Linq.

 quads.OrderBy(quads => quads.transform.position.y).ToArray();

the OrderBy method will declare a variable that you tell to be called quads and this is conflicting with your previous quads declaration.

So you need to change that new naming:

 quads.OrderBy(quad => quad.transform.position.y).ToArray(); 

And that should do it. Nonetheless, Linq is not recommended in the Update, Linq is handy but it also uses more memory and computation that a sort method.

You could limit the computation by only calling the method every x second with InvokeRepeating for instance and if you do not add any new quad then no need to call the FindObjectsWithTag other than in the Start.

 void Start(){
     quads = GameObject.FindGameObjectsWithTag("quad");
     InvokeRepeating("OrderQuad", 1f, 1f);
 }
 
 void OrderQuads(){
     quads = quads.OrderBy(quad => Vector3.Distance(transform.position, quad.transform.position)).ToArray();
 }

This will call every second, also, the OrderBy method is using the distance between the object holding the script and the objects from the quads array. Change the Distance if it is not what you needed.

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 slader155 · Jun 23, 2014 at 02:00 PM 0
Share

That's exactly what I was looking for, thanks!!!

avatar image
-4

Answer by Jeff-Kesselman · Jun 22, 2014 at 08:46 PM

Googling "C# Array Sort" quickly finds you your answer

http://msdn.microsoft.com/en-us/library/bzw8611x.aspx

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 Maerig · Jun 23, 2014 at 05:53 AM 2
Share

Like this

 private int CompareYPos(GameObject a, GameObject b) {
     return $$anonymous$$ath.Sign(a.transform.position.y - b.transform.position.y);
 }
 
 void Update() {
     quads = GameObject.FindGameObjectsWithTag("quad");
     Array.Sort(quads, CompareYPos);
 }

It would probably easier to iterate over the array only once and just return the GameObject with the highest transform.position.y, though.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Quaternion.RotateTowards for camera positions is buggy 1 Answer

How to create a step by step array to inrease y position? 3D 0 Answers

How to make instances interact/communicate with each other? 0 Answers

How to make 5 GameObjects swap between 5 predetermined positions (1 gameobject occupy 1 position per time)? 1 Answer

unknown y axis 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