Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed May 01, 2013 at 07:06 AM by Fattie for the following reason:

Duplicate Question

avatar image
0
Question by YayGee · May 01, 2013 at 03:02 AM · destroyspawning

Removing gameobjects 3 seconds after spawning

So I downloaded this script for spawning objects. It works really well but there is a problem: the objects that get spawned never gets removed, which creates lots of lag. Of course I understand that they wouldn't randomly get removed without me programming a single code but i don't really know how I am supposed to use the Destroy function to do what i want. Here is the script:

var prefabToSpawn:Transform;

var spawnTime:float;

 var spawnTimeRandom:float;

 private var spawnTimer:float;
 
 function Awake()
 {
     ResetSpawnTimer();
 }
 
 function Update()
 {
     if(spawnTimer > 0)
     {
         spawnTimer -= Time.deltaTime;
 
         if(spawnTimer <= 0.0)
         {
             spawnTimer = 0;
             Instantiate(prefabToSpawn, transform.position, Quaternion.identity);
             ResetSpawnTimer();
         }
     }
 }
 
 function ResetSpawnTimer()
 {
     spawnTimer = spawnTime + Random.Range(0, spawnTimeRandom*100)/100.0;
 }
Comment
Add comment · Show 2
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 YayGee · May 01, 2013 at 02:45 AM 0
Share

Note: I failed at posting the code. The two first rows are also a part of the script.

avatar image Fattie · May 01, 2013 at 06:36 AM 0
Share

Then could you use the EDIT button to fix it ?

3 Replies

  • Sort: 
avatar image
1
Best Answer

Answer by Eric5h5 · May 01, 2013 at 03:49 AM

The Destroy function can take a parameter saying how long it will be before the object is destroyed. Also that script would be made a lot simpler by using a coroutine.

 var minSpawnTime = 1.0;
 var maxSpawnTime = 10.0;
 var prefabToSpawn : GameObject;
 var prefabLifetime = 3.0;
 
 function Start () {
     while (true) {
         yield WaitForSeconds (Random.Range (minSpawnTime, maxSpawnTime));
         Destroy (Instantiate (prefabToSpawn, transform.position, Quaternion.identity),
                  prefabLifetime);
     }
 }
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
1

Answer by robertbu · May 01, 2013 at 03:49 AM

You can call Destroy() with a future time parameter, so you can call Destroy() on your just created game object:

 var go : GameObject = Instantiate(prefabToSpawn, transform.position, Quaternion.identity);
 Destroy(go, 3.0);




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 trs9556 · May 01, 2013 at 03:12 AM

Create a script that has this in it, then attach this script to the prefab you are instantiating:

 private var Spawntime : float;
 public var TimeInSecondsTillDestroyed : float;
 
 function Awake(){
     SpawnTime = Time.time + TimeInSecondsTillDestroyed;
 }
 
 function Update(){
     if(Spawntime < Time.time){
         Destroy(this.GameObject);
     }
 }


Once added, in the inspector, put the amount of time you want to wait till it destroys itself.

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 Eric5h5 · May 01, 2013 at 03:49 AM 0
Share

Destroy can take a time parameter, so you don't need that.

Follow this Question

Answers Answers and Comments

15 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

Related Questions

Spawning Children and Destroying them. 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Destroy only selected object 1 Answer

How to delete just one object, both with the same script,How to not delete both objects in a collision? 0 Answers

How to destroy linked components when object is destroyed? 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