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 @rppe · Sep 01, 2012 at 05:20 PM · musicsortfindwithtag

How do I sort Game Objects from closest to furthest, by tags?

I am working on a music game and I have an editor. I place notes down that follow a song. These notes all contain the information to be created in their name, so I need to write a file with their names. I need their names written in order from closest to furthest away, because it will improve the performance of my game. Now the question:

Does anyone know a way to iterate through a loop that will sort these Game Objects from closest to furthest? You should note that I am using findwithtag to find them all. I considered using a list, but I cant think of an efficient way to do that. Does anyone know a cost effective (in processing time) way of doing this? Please and Thank You!

P.S. psuedo works great for me too. Possibly better, I like to learn.

Comment
Add comment · Show 3
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 fafase · Sep 01, 2012 at 05:37 PM 0
Share

closest to what? the tone of the song?

avatar image @rppe · Sep 01, 2012 at 05:48 PM 0
Share

Thanks fattie (is that meant to be sound rude? :p ) but it hasnt helped much, Ive looked into it already. And @fafase, I need them closest to the start of the song. so I will explain a bit more for others. the name of each object is structured like this: R,2.0451 In that example, a red note (R) is created when the song passes 2.0451 seconds. In the script that reads the text files, it splits the name into two, at the comma. It then tests the second part to see if its larger or equal to the time of the song. Are you with me? :p Anyway, the problem is that they get written to the file in the order the findwithtag returns them in, so it will show a note created at 4 seconds, and one at 3 seconds after that. I could test each note always, but that would slow it down. I thought if I could test them in order, it would be more efficient. That is whats.

Long answer short, closest to 0, furthest from 0. thats what I would like.

avatar image @rppe · Sep 02, 2012 at 01:43 AM 0
Share

So I went ahead and tried a method. I will attach it, if people can help though:

 function WriteFile(){
     //the file path
     var sW : StreamWriter = new StreamWriter(Application.dataPath + "/" + SongName+"_"+selStrings[selGridInt]+".txt");
     //the notes that will be written into the file
     var Notes = GameObject.FindGameObjectsWithTag ("Note");
     //each note found has its own line written
     var Names = new Array();
         //The array called Names holds each note found
     for (var Note in Notes) {
         Names.Add(Note);
     }
         
         /*This is where it went wrong, right below.
         What I am trying to do is sort these notes by
         the time that they are created, and this is
         data stored in its name. the problem is that the
         name isnt just a float. it also stores the notes
         colour. It is comma seperated. I can split it up
         and sort half of it but then what? */
 
     Names.Sort();
     for (var Name in Names) {
         sW.WriteLine(Name);
     }
     sW.Flush();
 }

So how do I sort the list? It looks like this

 2.612649,O
 6.128291,B
 3.572816,R

@Fattie maybe you can help again? Your recent answer got me trying harder!

1 Reply

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

Answer by Bunny83 · Sep 02, 2012 at 01:58 AM

Well, your code contains two problems ;)

First, the Array class. It's slow but the worst thing is it's not type safe. You store GameObjects in this container. A GameObject can't be compared to another GameObject. You want to compare the GameObject "names" which are strings.

So my advice is: Use a List instead of Array and store the names in the list and not the Gameobjects:

 var Notes = GameObject.FindGameObjectsWithTag ("Note");
 var Names = new List.<String>(Notes.length); // set initial length for better performance
 for (var Note in Notes) {
     Names.Add(Note.name);  // store the name of the gameobject
 }
 Names.Sort();

edit To use the generic list you have to import the System.Collections.Generic namespace at the top of your script:

 import System.Collections.Generic;
Comment
Add comment · Show 6 · 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 @rppe · Sep 02, 2012 at 02:24 AM 0
Share

@Bunny83 I havent really dealt with lists in javascript before, and I keep getting the error

 BCE0005: $$anonymous$$ identifier: 'List'.

but on the bright side, I understand how it should work and I dont see the problem in the code.

avatar image @rppe · Sep 02, 2012 at 04:02 AM 1
Share

Ah! I just had to

 import System.Collections.Generic;
avatar image Bunny83 · Sep 02, 2012 at 11:00 AM 0
Share

Yes, you're right, advise - verb; advice - noun ;) I will fix this :D

I wouldn't say one in 10k. If you talk about the "internet generation" then yes, they actually dropped their language :D

avatar image Bunny83 · Sep 02, 2012 at 11:06 AM 1
Share

@@rppe: Great you figured it out. I'm actualy impressed :D $$anonymous$$ost people here can't do a step without a clear "advice" ;)

But i will add the import since it shouldd help others as well.

avatar image fafase · Sep 02, 2012 at 04:16 PM 0
Share

Yes, let's get back to program$$anonymous$$g as I do not agree with the French part and I really do know what I am talking about.

Show more comments

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

10 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

Related Questions

Unity: Attaching a volume control slider that doesn't always exist, to another object. 0 Answers

Spawning based on Music 1 Answer

How to stop audio in a scene when DontDestroyOnLoad was already called? 1 Answer

what is the best way to make game overworld music? 1 Answer

How do I Sort a List of Classes by a property? 2 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