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 Macarse · May 16, 2012 at 05:30 PM · performancedestroyprofiler

Best approach for a distance based destroyer

I am creating a game which the player collect coins and keeps going up. I need this coins to disappear when the user is too far.

What I am doing right now is adding a component to the coins with the following:

 public class PlayerDistanceBasedDestroyer : MonoBehaviour
 {
     private Transform target;
     
     void Awake()
     {
         this.target = GameObject.Find("Player").transform;
     }
 
     void LateUpdate ()
     {
         if ( (this.target.position - this.transform.position).y > PlayerFalling.FALLING_WARNING_DISTANCE )
         {
             ObjectPoolManager.DestroyPooled( gameObject );
         }
     }
 }


Since the profiler showed that it was called a lot I changed this to the following:

 public class PlayerDistanceBasedDestroyer : MonoBehaviour
 {
     private Transform target;
     
     void Awake()
     {
         this.target = GameObject.Find("Player").transform;
     }
 
     void onEnable()
     {
         StartCoroutine("DestroyLogic");
         
     }
 
     void onDisable()
     {
         StopCoroutine("DestroyLogic");
     }
 
 
     private IEnumerator DestroyLogic()
     {
         while (true)
         {
             if ( (this.target.position - this.transform.position).y >
                 PlayerFalling.FALLING_WARNING_DISTANCE )
             {
                 ObjectPoolManager.DestroyPooled( gameObject );
             }
 
             yield return new WaitForSeconds(1f);
         }
     }
 }


While testing this second implementation it felt slower (Nothing is said in the profiler). Do you think there is a better approach?

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

1 Reply

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

Answer by Befall · May 16, 2012 at 08:11 PM

First off, I'm unfamiliar with the Object Pool Manager use, but for each one, why not just use Destroy(gameObject)? Also, try testing out a single script, possibly a Coins script or something, iterating through the coins each frame, instead of each coin having a script itself that is called every frame.

No matter what, you'll need to check every frame for each coin, it just may be easier to do something like this:

 List<GameObject> toDestroy = new List<GameObject>();
 foreach (GameObject coin in coins)
 {
     if (Vector3.Distance(playerRef.transform.position, coin.transform.position) > maxDistance)
         toDestroy.Add(coin);
 }
 foreach (GameObject destroyCoin in toDestroy)
 {
     coins.Remove(destroyCoin);
     Destroy(destroyCoin.gameObject);
 }

The reason for the second list thingy is because you can't be iterating through a list and adjust its members. This is just a rough guess and may not suite you, but it would keep all your necessary code in one spot for coin removal.

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 Befall · May 16, 2012 at 08:13 PM 0
Share

También, si nunca has usado un List antes, tienes que incluir "using System.Collections.Generic" al principio de su code.

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

Instantiate vs caching GameObject in scene 3 Answers

Understanding the profiler 1 Answer

Profiler Windows - What is "Others" 1 Answer

What could be causing my super low performance on my Android tablet? 3 Answers

Huge "other" in Gpu Profile ! 4 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