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
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
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
function Update ()
{
Destroy (gameObject, 5) //5 is how many seconds you want before the object deletes itself
}
When u need to delete a object say in 4 seconds, use a coroutine. Yield for 4 seconds then delete the object.
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