Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 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 Lemo76 · Jul 24, 2015 at 12:07 PM · javascriptlistoptimization

Check if List contains specific object values (JS)

I'm trying to optimise my code:

 for (var a = 0; a < GhostObjects.Count; a++) {
    if (GhostObjects[a].name == "GhostObj Pos_" + Current_Pos && GhostObjects[a].transform.position.x < 1) {
       Death();
    }
 }

As you can see, I'm going through a list and checking to see if there is an object with a name of "GhostObj Pos_34" (If the current pos was 34), and then I'm checking to see if that object has a position of x less than 1.

If both criteria is matched, it will then go ahead and kill the player.

Here lies the problem: as the game progresses, there are more and more Ghost Objects added to the list, and therefore the code becomes slower because it iterates through all of the ghost objects.

Please can you suggest ways I can get around this problem by optimising the code to run at a constant speed whatever the case maybe.

Please note, I have tried List.Contains, but I don't know how I would insert multiple criteria of the same object into the contains.

Thanks!!

Comment
Add comment · Show 4
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 Dave-Carlile · Jul 24, 2015 at 12:46 PM 0
Share

How many objects are we talking? And have you used the profiler to see if this is indeed a problem?

avatar image Lemo76 · Jul 24, 2015 at 12:59 PM 0
Share

Around 45 ghost objects. The profiler points directly to this update sequence. I disabled it and it ran smooth constantly.

avatar image Dave-Carlile · Jul 24, 2015 at 01:11 PM 1
Share

Hmmm, 45 is nothing really. How is GhostObjects being populated? Doing a Find every frame could be an issue.

Can multiple objects have the same name? If there's just a single ghost at any position, you should be able to find that in the list directly, then just compare x... (this is C#, so convert to JS as needed)

 int i = GhostObjects.IndexOf("GhostObj Pos_" + Current_Pos);
 if (i >= 0)
 {
   if (GhostObjects[i].transform.position.x < 1)
   {
     Death();
   }
 }


An arguably better option is to set up triggers on your objects and let Unity notify you of collisions.

avatar image Lemo76 · Jul 24, 2015 at 11:17 PM 0
Share

Only problem is that since it's a list of objects, GhostObjects.IndexOf("GhostObj Pos_" + Current_Pos); should be searching for gameobjects with that name rather than that specific string.

I'm unsure how to convert the list of objects to a list of names of the objects (strings) using the least amount of performance (i.e. Avoid creating another list).

I've been trying things like:

 print (GhostObjects.Find(obj => string.Equals(obj.name, "GhostObj Pos_" + Current_Pos ));

..or..

 print (GhostObjects.Find(obj=>obj.name=="GhostObj Pos_" + Current_Pos));

and it tells me that obj is an unexpected token

1 Reply

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

Answer by fuego_see_money · Jul 24, 2015 at 11:46 PM

You should just make a script, called GhostDeath or something, and attach it to every Ghost object. Much simpler, and doesn't require a for loop.

Create a empty game object with a bounding box where you want the Ghosts to delete, and then tag it "`DeathBox`". Then, just have each Ghost check for collisions with it:

 void OnCollisionEnter(Collision c)
 {
     if(c.gameObject.tag == "DeathBox")
     {
         Die();
     }
 
 }

It's also more object oriented (the objects are checking themselves for death).

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 fuego_see_money · Jul 24, 2015 at 11:47 PM 0
Share

O$$anonymous$$G I suck. Im sorry, I forgot this was a Javascript question...

avatar image Lemo76 · Jul 24, 2015 at 11:58 PM 0
Share

Haha, no that's fine. I'll see if it works out (I've been avoiding collision/trigger detection because all movement is coded via rotation)..

avatar image Lemo76 · Jul 25, 2015 at 12:45 AM 0
Share

Remodified all my scripts, and now I'm using collision detection. Works like a charm.

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

23 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

Related Questions

Java Lists 1 Answer

A node in a childnode? 1 Answer

Javascript Enum Write List 0 Answers

How long should it take me to make my game 2 Answers

JS Argument out of range 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