Destroy instance clone of gameobject

I am creating a small interaction where you click on a table and a small animation plays in response. What I’m getting caught up on is that when I instantiate the click I cannot rid the click object from the game. This has ended up with me bogging it down with too many objects in scene. if you guys could take a look at my code I would greatly appreciate it! As I have been looking to the answer to this problem for hours now haha!

the fist block of code is in java script and the second is in c#

#pragma strict

var touch : GameObject;

function OnCollisionEnter (other : Collision) {
//public float initializationTime;

if (other.transform != transform && other.contacts.length!=0)
{
	for (var i = 0; i < other.contacts.length; i++)
	 {
	Instantiate(touch, other.contacts*. point,Quaternion.identity);*
  • Destroy(GameObject.Find(“Click(Clone)”),10);*
  • } *
  • }*
    using UnityEngine;
    using System.Collections;

public class ImageSequenceTextureArray : MonoBehaviour
{

  • //An array of Objects that stores the results of the Resources.LoadAll() method*

  • private Object objects;*

  • //Each returned object is converted to a Texture and stored in this array*

  • private Texture textures;*

  • //With this Material object, a reference to the game object Material can be stored*

  • private Material goMaterial;*

  • //An integer to advance frames*

  • public int frameCounter = 0;*

  • void Awake()*

  • {*

  •  //Get a reference to the Material of the game object this script is attached to* 
    
  •  this.goMaterial = this.renderer.material;* 
    
  • }*

  • void Start ()*

  • {*

  •  //Load all textures found on the Sequence folder, that is placed inside the resources folder* 
    
  •  this.objects = Resources.LoadAll("Sequence", typeof(Texture));* 
    
  •  //Initialize the array of textures with the same size as the objects array* 
    
  •  this.textures = new Texture[objects.Length];* 
    
  •  //Cast each Object to Texture and store the result inside the Textures array* 
    
  •  for(int i=0; i < objects.Length;i++)* 
    
  •  {* 
    

this.textures = (Texture)this.objects*;*
* }*
* }*

* void Update ()*
* {*
* //Call the ‘PlayLoop’ method as a coroutine with a 0.04 delay*
* StartCoroutine(“Play”,0.00f);*
* //Set the material’s texture to the current value of the frameCounter variable*
* goMaterial.mainTexture = textures[frameCounter];*

* }*

* //The following methods return a IEnumerator so they can be yielded:*
* //A method to play the animation in a loop*
* IEnumerator PlayLoop(float delay)*
* {*
* //Wait for the time defined at the delay parameter*
* yield return new WaitForSeconds(delay);*

* //Advance one frame*
* frameCounter = (++frameCounter)%textures.Length;*

* //Stop this coroutine*
* StopCoroutine(“PlayLoop”);*

* }*

* //A method to play the animation just once*
* IEnumerator Play(float delay)*
* {*
* //Wait for the time defined at the delay parameter*
* yield return new WaitForSeconds(delay);*

* //If the frame counter isn’t at the last frame*
* if(frameCounter < textures.Length-1)*
* {*
* //Advance one frame*
* ++frameCounter;*
_ /
if(frameCounter >= 400)
{*_

* objects =null;*
* textures = null;*
* goMaterial = null;*
* frameCounter = frameCounter - 400;*

_ } /
}
Destroy(GameObject.Find(“Touch”),10);
//Stop this coroutine*
* StopCoroutine(“Play”);
}*_

}

Instantiate returns the instantiated object, so you can just call Destroy directly on it when you spawn the object:

 Destroy(Instantiate(touch, other.contacts*. point,Quaternion.identity), 10);*

You’re also trying to destroy the gameobject in the Play-coroutine. If you want to destroy it there, reference it directly instead of trying to Find() it:
IEnumerator Play(float Delay) {

Destroy(gameObject);
}
You’re also calling StopCoroutine(“Play”) at the end of the Play-coroutine. This is redundant - the coroutine will stop when you get to the end of it. The same goes for the PlayLoop routine.
Also, when you’re starting the Play-coroutine, you’re sending in a delay of 0 seconds, while claiming that you’re playing it with .04 seconds in the comment.

Thank you so much for your help I’ll be incorporateing that into the code! Now I do have another question about what you’ve said. With the play loops, if I were to remove the loop it will play only once, every time it’s instantiated, right? I’ve had some issues where I click, and it only plays once for the first clone and all clones created afterwards it registers touch but stops any animation. It mainly happens once I change the courutiens to play instead of playLoop. Thank you for the quick reply and I hope to hear back from you!