- Home /
How to delete things with a timer
I'm trying to make a timer to where after a certain amount of time the bullet gets deleted that way there arnt a billion bullets just going and using up cpu and ram
Answer by Borgo · Jun 28, 2011 at 07:59 PM
You can create a simple script that you can attach to wherever object you want:
var secondsToDestroy: int;
function Awake(){
Destroy(this, secondsToDestroy);
}
I did'n test yet. Attach this script to any object and set the "destroyOnTime", this is the time that the object will survive XD
gives me this error - Assets/destroybullet.js(7,16): BCE0023: No appropriate version of 'UnityEngine.Object.Destroy' for the argument list '()' was found.
Also, I'm starting to have a change in $$anonymous$$d how would I do raycasting for bullets?
You didn't look at that link I posted. Go look. Destroy takes some arguments.
DaveA is right, the Destroy function supports a Time argument... Destroy(object, seconds);
Answer by DaveA · Jun 28, 2011 at 07:47 PM
Bullets! So many questions, so few people using Raycasting instead!
Anyway, use Object.Destroy when you are done with them. Example here: http://unity3d.com/support/documentation/ScriptReference/Object.Destroy.html
How would I destroy it after a certain amount of time?
See the example on that page: // $$anonymous$$ills the game object in 5 seconds after loading the object Destroy (gameObject, 5);
Answer by FPSNOOB:D · Nov 03, 2012 at 08:29 AM
function Update ()
{
Destroy (gameObject, 5) //5 is how many seconds you want before the object deletes itself
}
Answer by Soareverix · Jun 23, 2018 at 11:52 PM
Use this function, just make a C# script. --- using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ObjectDestruction : MonoBehaviour {
public GameObject item;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
NewMethod();
}
private void NewMethod()
{
Destroy(item, 15); //5 is how many seconds you want before the object deletes itself
}
}
This is literally all you have to do, just input the GameObject you want to destroy into the variable in Unity. Enjoy!,Create a C# script and literally copy and paste this into into it. Afterwards, select the GameObject you want to destroy.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class ObjectDestruction : MonoBehaviour {
public GameObject item;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
NewMethod();
}
private void NewMethod()
{
Destroy(item, 15); //you have 15 seconds before the objects deletes itself
}
}
This is the easiest way :)
Answer by gamerkaa · Jun 24, 2018 at 04:02 PM
When u need to delete a object say in 4 seconds, use a coroutine. Yield for 4 seconds then delete the object.
Your answer
Follow this Question
Related Questions
Firing bullets deleats player not bullet 1 Answer
Trying to deleate bullets once they leave the screen 1 Answer
Need help with randomize attack time. Please help! 2 Answers
Bullet Effect 2 Answers
Problem With Bullet Prefab 2 Answers