• 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
1
Question by cka123 · Feb 06, 2017 at 07:59 AM · generationdestroygameobject

How can i destroy generated(clones) obstacles

The obstacle are generated using a generate script and gameobject is in the prefabs folder i have attached the gameobject with the generate script the problem is that it wont get destroyed

using UnityEngine; using System.Collections; public class Generate : MonoBehaviour { public GameObject rocks; int scr;

     // Use this for initialization
     void Start()
     {
         InvokeRepeating("CreateObstacle", 3f, 3f);
     }
     void OnGui()
     {
         GUI.color = Color.black;
         //GUILayout.Label ("Score is" + scr.ToString ());
     }
 
     void CreateObstacle()
     {
         Instantiate(rocks);
         scr++;
 
     }
     void OnBecameInvisible() { 
         // Destroy the bullet 
         Destroy(rocks);
         Destroy(this.gameObject);
     } 
 
 }


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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TomArano_Dimenco · Aug 28, 2020 at 08:28 AM

you could add a 'destoryaftertime' script to your bullets, its what i usually do

just add a float u can edit from inspector and add that to the destroy function that u can simply call from start() or awake()

alt text


destroydelay.png (7.7 kB)
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 Llama_w_2Ls · Aug 28, 2020 at 10:51 AM 0
Share

Unfortunately, I believe he wanted to destroy the clones when running a funtion and not just destroying it after a set period of time.

avatar image
0

Answer by datpn2k8 · Aug 28, 2020 at 04:10 AM

just make side camera colider and when it touch, it destroy

 void OnColider(Colider col){
      if(col.name == "ScreenSide")// That the block u placed left to the camera
      {
           //Destroy
      }
 }



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 ShadyProductions · Aug 28, 2020 at 09:32 AM 0
Share

This question was posted 3 years ago, answering it is pointless. And especially with the wrong answer.

avatar image
0

Answer by Llama_w_2Ls · Aug 28, 2020 at 10:49 AM

The reason why its not destroying is because you are trying to destroy the original prefab. You cant destroy prefabs as its not safe to do so, in case of data loss, since its not in the scene. To destroy clones, you need to find the clone objects, either by searching for all the gameobjects called rocks(Clone) and destroying those, or you can tag all generated clones right after theyre instantiated and destroy all objects with that tag. This is just for anyone else who needs a solution for this question as the asker probably wont see it.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

,Can i use InvokeRepeating to spawn just 1 GameObject at a time? 1 Answer

how to destroy an object when it was hit with particle 0 Answers

creating a mesh procedurally 4 Answers

This script really makes my game lag! 1 Answer

How to create an endless track? 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