- Home /
I need one of my prefabs to be deleted after a few seconds
Code 1: (Bullet Destroy)
#pragma strict
var DestroyTime : float;
function Awake () {
Destroy(GameObject.Find("Projectile(Clone)"), DestroyTime);
}
Code 2: (Bullet Spawn) #pragma strict
var bulletspawn : Transform;
var bulletprefab : GameObject;
var bulletSpeed : float;
function Start () {
}
function Update () {
if (Input.GetMouseButtonUp(0)) {
var bulletinstance = Instantiate(GameObject.Find("Projectile"),GameObject.Find("BulletSpawn").transform.position,Quaternion.identity);
bulletinstance.rigidbody.AddForce(transform.forward * 2000);
}
}
Whenever I click left mouse button, my bullets spawn like normal, with a force of 2000 on the rigidbody. The original prefab is not deleted, but the cclones are, which is good.Whenever i spawn 2 or more bullets at the same time, none of them are deleted. How can I fix this?
This is not a wise approach to scene or instance management.
First, objects like projectiles should generally be pooled for efficiency; plenty of info about this concept floating around.
Often the pooling concept gives you a convenient chance to store and manage both prefabs and instances. Using the Find() family of methods should always be a last resort; they're expensive.
If you don't want to get into all this right now, just create a script on your bullet prefab that self-destructs after N seconds:
Do not keep your prefab alive in the scene; it should only exist in your project's assets as a prefab.
void Start(){
Destroy(gameObject, DestroyTime);
}
I did what you said, and changed the code:
Bullet Destroy:
#pragma strict
var DestroyTime : float;
function Start () {
Destroy(gameObject, DestroyTime);
}
Bullet Spawn:
#pragma strict
var bulletSpeed : float;
function Update () {
if (Input.Get$$anonymous$$ouseButtonUp(0)) {
var bullet = Instantiate(gameObject,GameObject.Find("BulletSpawn").transform.position,Quaternion.identity);
bullet.rigidbody.AddForce(transform.forward * 2000);
}
}
I moved both the bullet spawn and bullet destroy scripts to the prefab, after deleting the prefab from the scene. Now the Projectiles do not spawn at all. Once I moved the scripts, i changed the destroy time to 7 seconds and the speed to 2000, but it still doesn't spawn them. What do I do to fix this?
Also, if the above comment cannot be fixed for some reason, what is "pooling" like you mentioned earlier?
This entire question was asked because of this youtube series: http://adf.ly/1AO5Cm (episode 5 is where i encountered the problem)
You should spend some time looking over Unity's on-site tutorials and lessons. A bullet should not spawn itself, nor should it need to Find() the location from which to spawn. If this approach was suggested by a youtube video, I wouldn't recommend following their advice.
Ins$$anonymous$$d, some other object (like a Player or a Gun) should handle spawning by keeping a reference to the bullet prefab.
// on your gun/player/turret/whatever script, assign the prefab by click-n-dragging it from your assets folder
public GameObject bulletPrefab;
Pooling is a concept used to reduce the cost of spawning objects at runtime. There's a lot written on the subject, so finding more information should be easy. Search for "Unity Object Pooling" or something. Don't worry about this yet though; learn your fundamentals first.
Answer by TSRajesh · Mar 19, 2015 at 03:59 AM
I use Invoke for my arrows.. Thing is, i don't want to delete them if it sticks somewhere.. but self-destroy if didn't hit anywhere.. Works OK for me..
public class Arrow : MonoBehaviour {
void Start()
{
Invoke("DestroyMe", 10);
}
void OnTriggerEnter(Collider other) {
CancelInvoke("DestroyMe");
//Blah Blah
}
void DestroyMe()
{
Destroy(transform.gameObject);
}
}
ok, and what script do you use to spawn them? where do you attach this script too?
I would say the script is a component of the arrows themselves, considering the name of the class and the fact that Destroy() is called on the gameobject the script is attached to.
Yes, this script is attached to the arrow.. In the prefab. I spawn it with a simple "instantiate" command.. The usual stuff when you create an object from a prefab.. I do that in my bow script, when my "fire" key is pressed..
var arrowobject : GameObject;
arrow = Instantiate(arrowobject, gameObject.transform.position, Quaternion.identity);
Is that similar to the code that you use?