destroying prefabs after instantiation scripts

Hi, I’m trying to have prefab music notes come out of my guitar melee weapon and it’s working. I’d like the notes to be destroyed after a few seconds, but they stay in the scene.

I tried adding a destroy game object line in the java melee script after instantiation of the musicnote, but it only destroyed the guitar.
Then I tried adding a destroy gameobject script, this time in C# to the prefab musicnote.

No errors popped up but the musicnote clones just keep coming and none get destroyed. Any feedback on what I’m doing wrong would be great.

#pragma strict

var TheDamage : int = 50;
var Distance : float;
var MaxDistance : float = 1.5;
var Mockron_Guitar : Transform;
var MusicNote : GameObject;
function Update()
{
        if (Input.GetButtonDown("Fire1"))
        {
        animation.Play ("Melee");
        }

var hit : RaycastHit;
if (Physics.Raycast (transform.position, transform.TransformDirection (Vector3.forward), hit))
{
    Distance = hit.distance;
    if (Distance < MaxDistance)
    {
    hit.transform.SendMessage("ApplyDamage", TheDamage, SendMessageOptions.DontRequireReceiver);
    var instance : GameObject = Instantiate(MusicNote, transform.position, transform.rotation);
    }
}
}
using UnityEngine;
using System.Collections;

public class Destroy : MonoBehaviour {

    GameObject MusicNote;


    // Update is called once per frame
    void Start () {

        Destroy (gameObject);
    }
   
}

You should be able to simply register the object to be destroyed in a few seconds - immediately after creating it.

So, right after you instantiate the new object, just add something like:

Destroy (instance, 2);

Jeff

Thanks Jeff, that worked. Thanks for teaching me that!