• 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

How do I get my game to run faster? 4 Answers

Profiler Alpha Mobile 0 Answers

Shader.Parse overhead 0 Answers

Active rigidbodies and number of Contacts 1 Answer

GFX.WaitForPresent takes the most time? 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges