Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Destroy and Re-Instantiate objects when they are out of camera

Discussion in '2D' started by xliquid, Jul 7, 2020.

  1. xliquid

    xliquid

    Joined:
    Jan 29, 2020
    Posts:
    6
    So I basically want to destroy objects when they are out of the camera but I re-instantiate it when the object can be seen by the camera again, I can't think of a performant way to do this, thanks in advance for any answer :D
     
  2. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Well, I think thats not a good idea since Unity doesn't graph objects outside of camera automatically (unless you unset the option). Since you say u want to reinstantiate it, I assume it's an static object with no movement or any AI, since you also will destroy any scipt attached to it.

    A most common practice is, instead of destroy and instantiate all the objects. Make the same with a pool of objects. If the game is lineal, you can Destroy/Instantiate it when player makes an event, if it's an open world, you would need to see the transform.position and the transform.position of your camera... or if the gameObject has some Renderer:
    Code (CSharp):
    1.     private void OnBecameInvisible()
    2.     {
    3.         Debug.Log("I'm invisible");
    4.     }
    5.  
    6.     private void OnBecameVisible()
    7.     {
    8.         Debug.Log("I'm here");
    9.     }
    But remember, if you destroy the gameObject OnBecameInvisible(), he will not be able to became visible again
     
    xliquid likes this.
  3. xliquid

    xliquid

    Joined:
    Jan 29, 2020
    Posts:
    6
    Thanks for the quick answer, I already tried setting my objects active/deactivating them using these methods, deactivating worked, but reactivating didn't since the object is deactivated and can't interact with the script, do you know a workaround for this?
     
    Last edited: Jul 7, 2020
  4. rubcc95

    rubcc95

    Joined:
    Dec 27, 2019
    Posts:
    222
    Well if you wanna do it with OnBecame(In)Visible functions, attach each GameObject you wanna to deactivate as a child of a parent called, for example, Deactivator. And Deactivator would have this script attached.

    Code (CSharp):
    1. public class Deactivator : MonoBehaviour
    2. {
    3.     GameObject target;
    4.  
    5.     void Awake()
    6.     {
    7.         target = transform.GetChild(0).gameObject;
    8.     }
    9.  
    10.     private void OnBecameInvisible()
    11.     {
    12.         target.SetActive(false);
    13.     }
    14.  
    15.     private void OnBecameVisible()
    16.     {
    17.         target.SetActive(true);
    18.     }
    19. }
    And you would deactivate the whole GameObject, just keeping the parent which only has attached the script for reactivate it. But as I listened to (I'm not sure about this, never test what works better) the pool system I talked at the post above is a better practice.
     
    xliquid likes this.
  5. xliquid

    xliquid

    Joined:
    Jan 29, 2020
    Posts:
    6
    thanks that helped, and sorry that it took so long to reply :D