Dissolve On Destroy

I’ve made a script which goes with a shader i made to make an object dissolve and then destroy it, although I want to change it so that it will dissolve then destroy after it has been hit with a bullet with the tag “Bullet”

function Update() {

renderer.material.SetFloat( “_SliceAmount”, 0.4f * Mathf.Abs(Mathf.Sin(Time.time)*0.6 ));
Destroy (gameObject, 1.5);
}

Well, you probably don’t want to dissolve/destroy your object in every single Update frame. Since the Update function wants to more or less perform all of its code in the same frame, it won’t wait till the dissolve is finished to destroy the object, creating troubles and/or crashes. That’s why you can’t use a yield statement in an Update function. Instead, you might be better off placing the code you need in a separate function then calling it when you need it… like so:

function DissolveAndDestroy () {

     // Do your dissolve thing
     renderer.material.SetFloat( "_SliceAmount", 0.4f * Mathf.Abs(Mathf.Sin(Time.time)*0.6 ));

     // Wait till it's finished dissolving
     yield WaitForSeconds (The AmountOfTimeDissovleTakes);

     // Destroy it
     Destroy (gameObject, 1.5);

}

I didn’t test ny of this, but it should give you the basic outline of how to get it working…

You would have to have 2 objects. Your bullet, and your ‘object’ which is going to dissolve. Then, simply check collision between the two objects.

With on collision enter: http://unity3d.com/support/documentation/ScriptReference/Collider.OnCollisionEnter.html

With that, you will know when they collide. Then you can set up a ‘flag’ boolean like:

var bBulletHit:boolean;

function Awake()
{
   bBulletHit = false;
}

function Update()
{
   if( bBulletHit )
   {
      //Your dissolve code
   }
}

function OnCollisionEnter(cInfo:CollisionInfo)
{
   //Your code to check if collided object is of type or name bullet (however you're checking)
   bBulletHit = true; //if it was a bullet, just flag this boolean, and you're done.
}

Look at. CollisionInfo on the unity script reference. There’s a way to access the tag reference of the object colliding. So you can know if it’s a bullet. Also it shouldn’t dissolve a little each time if you’re using an update loop like my code says. Hope that helps.

First, of course, you have to set up tags on objects (for how, go here: Unity tags Info),

After you set up your tag, you can access the name of the tag in an OnTriggerEnter function:

function OnTriggerEnter(col : Collider) {

   if(col.gameObject.tag == "nameOfYourTag"){
      //Do Something
   }

}