I know there’s many questions like this one here already, iv’e tried most of them and they doesn’t seem to work.
So, what I’m trying to do (as the title says) is to destroy a instantiated prefab to prevent lag.
This is my current code, which doesn’t work :
using UnityEngine;
using System.Collections;
public class Shooting : MonoBehaviour
{
public Rigidbody projectile;
public float speed = 20;
public float destroyTime = 3.0f;
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
//Shooting sounds should be played inside here.
Rigidbody instantiatedProjectile = Instantiate(projectile, transform.position, transform.rotation) as Rigidbody;
instantiatedProjectile.velocity = transform.TransformDirection(new Vector3(0, 0, speed));
Destroy(instantiatedProjectile.gameObject, destroyTime);
}
}
}