I’m triggering an animation that plays when the Enemy mesh is colliding with a Projectile mesh that is tagged.
The animation should play where the “Enemy” location is.
The script is attached to the Enemy.
The game object that has the animation is a 3DText mesh made into a prefab.
The animation plays but does not disappear.
How can I destroy the prefab (that plays the animation)?
If I do: Destroy(gameObject) it will destroy the Enemy only.
Do I need to set a clone for the prefab? And destroy the clone?
Thanx Guys!
public class Bonus_Animation : MonoBehaviour
{
//GameObject with the bonus animation attached
GameObject spawnBonus;
//bonus points at 50
public int points = 50;
void OnCollisionEnter (Collision col)
{
if(col.transform.tag == "Projectile")
{
Instantiate(spawnBonus, transform.position, transform.rotation);
}
}
}
You can add a script to your animation mesh that can read something like
var life : float;
function Start()
{
life = Time.time + 1.0;
}
function Update()
{
if(life <= Time.time)
{
Destroy(gameObject);
}
}
not tested but seems like it work. Let me know if it doesn’t.
Also just to help you understand my answer. At Start() life will equal whatever the current time is plus 1. Then in Update it'll keep checking for when life is smaller than the current time. Since we made life equal the current time plus 1 it'll be bigger then the current time at first. As time passes however it'll eventually become greater than life (in my example it'll take 1 second) and the object will be destroyed.
It works...thanx!
– PumaManMan
– vaibhavatul47InvalidCastException: Cannot cast from source type to destination type.Didn't work for me. I got above error.I tried this and some scripts similar but didnt work. like HOW??!
– cankirici34@cankirici34 share your code, how doesnt it work?
– Rioneer