- Home /
destroy a trail made by invoke repeat ?
i made a trail that spawns when an object is shot , but i cant seem to make it "destroy" , basically like angry birds , when u shoot the first chicken it draws a trail , you shoot the second one , it draws another trail and destroys the previous one , any idea how to achieve such a result ? here a code snippet
public GameObject[] trails;
void OnMouseDown()
{
IsPressed = true;
rb.isKinematic = true;
}
void OnMouseUp()
{
IsPressed = false;
rb.isKinematic = false;
StartCoroutine(Release());
InvokeRepeating("spawntrail", 0.05f, 0.05f);
}
void OnCollisionEnter2D(Collision2D collision)
{
CancelInvoke("spawntrail");
}
void spawntrail()
{
if (GetComponent<Rigidbody2D>().velocity.sqrMagnitude > 10)
{
Instantiate(trails[next], transform.position, Quaternion.identity);
next = (next + 1) % trails.Length;
}
}
Answer by spencerz · Feb 29, 2020 at 09:04 PM
by creating clones of the trails and instantiating the clones instead — we are able to destroy the clones. this has been tested with your code and it works.
void spawntrail()
{
if (GetComponent<Rigidbody2D>().velocity.sqrMagnitude > 10)
{
// create clones instead so we can destroy them
GameObject clone = Instantiate(trails[next], transform.position, Quaternion.identity);
next = (next + 1) % trails.Length;
Destroy(clone, 10F);
}
}
Your answer
Follow this Question
Related Questions
Invoke Ball Obstacle based on Score 0 Answers
Drawing a glowing line from one point to another on update 1 Answer
GL.Line help? Cannot see line drawn... draw grid using gl.line 4 Answers
Help : Need To Draw a Box 1 Answer
How to unsheathe sword 0 Answers