Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by wasicool7 · Aug 23, 2016 at 08:13 PM · cameraobjectdestroyspawn

Unity2D: How to destroy spawned object once it exit out of camera's view?

I'm trying to destroy spawned objects once it exit out of camera view automatically/straight away. You see I'm trying to make a flappy brids type of game but things also comes in from the bottom and if they get a really high score then things will be spawned in from above and from the left side. So, so far this is what my game looks like (I'm still working on it). However I'm having problems with deleting spawned objects once it exit out of camera's view, I tried all sorts of things like:

 private var hasAppeared : boolean; 

 function Start ()
  {    
 hasAppeared = false;
 }

 function Update() 
 {

 if (GetComponent.<Renderer>().isVisible)
 {
     hasAppeared = true; 
 }
 
 if (hasAppeared)
 {
     if (!GetComponent.<Renderer>().isVisible)
         {
             Destroy(gameObject);
         }
     }                

 }

But with this script, it doesn't destroy the game object quickly enough that and only some of them is being deleted. I also tried this method:

      public void OnBecameInvisible() {
          Destroy(gameObject);
       }

But not all of them is being deleted as well and it also takes a long time for any of my game objects to be destroyed. My last result was this script, it destroys the game object straight away once it exit out of the camera's view (which is what I want) but only these sides works where as the other's don't, I think it destroys my enemies (spawned objects) before it has even entered the camera's view. How do I get the script to work on this side? This is my script:

 void Update ()
 {
     DestroyAllEnemy ();
 }
 
 public void DestroyAllEnemy()
 {
     GameObject[] enemies = GameObject.FindGameObjectsWithTag ("enemy");
     if ((enemies.Length > 0) && (camera != null)) {
         for (int i = (enemies.Length - 1); i >= 0; i--) {
             // just a precaution
             if ((enemies [i] != null) && ((camera.WorldToViewportPoint (enemies [i].transform.position).x > 1.03f) || 
                 (enemies [i].transform.position.y < -6f))) 
             {
                 Destroy (enemies [i]);
             }
         }
     }
 }

Thank you :).

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
0

Answer by oStaiko · Aug 23, 2016 at 09:54 PM

The problem with your last one is that you only test for 2 sides, so of course it wont work for 4 sides...

Other than that, I think an easier solution than putting the destroy script on a GameController or such is to put the scripts on the enemies themselves. Just try attaching it to all enemies and make it look like this:

 public class DestroyByBoundary : MonoBehaviour
 {
     public float left;
     public float right;
     public float top;
     public float bottom;

     void Update()
     {
         if (position.x > right || position.x < left || position.y > up || position.y < down)
         {
             Destroy(this);
         }
     }
 }
Comment
Add comment · Show 2 · 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 wasicool7 · Aug 24, 2016 at 05:36 PM 0
Share

Thank you for your reply, Yeah I got this error: The name `position' does not exist in the current context. I change it to transform.postion and destroy (gameObject), becasue destroy(this) did destroy anything. But destroy(gameObject) just deleting/destroying all of my game object before it even gets a chance to leave the spawning point. Thank you for your help. :)

avatar image TBruce wasicool7 · Aug 24, 2016 at 05:51 PM 0
Share

I think @oStaiko meant transform.position.

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

90 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 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 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 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

Destroying a spawn object when it exit the main camera view not working 0 Answers

Spawning Objects from Inspector 1 Answer

I'm trying to spawn an object once another object has been destroyed how do i do that. 2 Answers

Object not visible with skybox 0 Answers

CameraController (object reference not set to an instance of an object) 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