• 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 corriedotdev · Mar 23, 2014 at 10:04 PM · javascriptgameobjectdestroyclone

Destroying a gameobject clone after a time

Here is my code for spawning multiple enemies forever. The level changes if they kill the player. How can i get it so that if the cloned objects collide with something then its deleted without causing errors i know to use something like this

 using UnityEngine;
 using System.Collections;
 
 public class trigger_Destroy: MonoBehaviour {
     void OnCollisionEnter2D(Collision2D coll) {
         if (coll.gameObject.tag == "floor")
             Debug.Log("destroying enemy...");
             Destroy(gameObject);
         
     }
 }

But of course it wont work as the object the script is using is being deleted.. im stumped really. Not to sure on where to go. Any help is always appreciated.

 var theObject:GameObject;
 var maxPosx:float = 2.898618; // x axis
 var minPosx:float = -1.89557; // x axis
 var delay:float = 0.5; // delay each spawned item by 0.5
 var maxPosy:float = 5.086665; // y axis
 var minPosy:float = 5.086665; // y axis
 
 
  
 function Start(){
     StartCoroutine(spawn());
 }
  
     function spawn() : IEnumerator {
         for (;;) {
            yield WaitForSeconds(delay);
            var theNewPos= new Vector3 (Random.Range(minPosx,maxPosx),Random.Range(minPosy,maxPosy),0);
            var go : GameObject = Instantiate(theObject);
            go.transform.position = theNewPos;
         }
 
     }

EDIT: What if i added

         Destroy(theObject);


Which would destroy the cloned object that isnt needed? Im not on unity now but would this work in theory?

If you know of a way that i can edit this script to remove the spawned objects after a time i would appreciate it.

Comment
Add comment · Show 1
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 nesis · Mar 23, 2014 at 10:12 PM 0
Share

Are you using a prefab as the thing to copy with Instantiate()? It seems to me like you have a GameObject sitting in the scene, assigned as the thing to copy.

3 Replies

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

Answer by corriedotdev · Mar 24, 2014 at 09:15 PM

Once again i have answered my own question. I needed to destroy the clone, not the object itself.

in this case it would be

 Destroy(go);
Comment
Add comment · 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
0

Answer by SkaredCreations · Mar 23, 2014 at 10:49 PM

Of course it's stopping the coroutine, because you're deleting the object itself that have attached the script. You should manage the spawn on a script attached to another object that is not going to be destroyed.

Comment
Add comment · 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
0

Answer by Fappp · Mar 24, 2014 at 09:39 PM

Turn it around! Make the floor kill the enemy.

Comment
Add comment · 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

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

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

Trying to make a simple inventory: 0 Answers

Destroy GameObject Variable? 3 Answers

Confused about GameObject and Transform 1 Answer

Erasing a gameobject if you have the coordinates. 3 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