Destroy Prefab clone

Hey everyone…

I am having one coin pattern prefab in that i have collection of coin with single parent object…

Now whenever my player collides with that coin i am destroying coin in player script. Nd for spawning coin i have another spawner script.

Now when i instantiate coin pattern next time, i could not find previously destroyed coin… means coins are destroying from prefab itself…

So for that what should i do?? Thanks for your support and help till now.

function FixedUpdate() {

var ray : Ray = Ray(player.transform.position, Vector3.up);
var hit : RaycastHit;
var somthingAboveMe : boolean = Physics.Raycast(ray, hit);

if (Physics.Raycast(ray, hit)) {
	if(hit.collider.name == "Coin")
	{
		if(hit.collider.gameObject.transform.parent.name.Contains("(Clone)"))
		{
			Debug.DrawLine (ray.origin, hit.point);
			player.gameObject.rigidbody.velocity = new Vector3(0, 0, 0);
			player.gameObject.rigidbody.AddRelativeForce(0 , 800 ,0);
			player.gameObject.layer = 8;
			ChangeChildLayerToPlayer();
			Destroy(hit.collider.gameObject);
		}
	}
}
}

You need to attach Destroy(gameObject); …with coin if collision occur … otherwise if you destroy clone it will destroy main gameObject…

var goHit : GameObject;

function FixedUpdate() {

goHit = null;
 
var ray : Ray = Ray(player.transform.position, Vector3.up);
var hit : RaycastHit;
var somthingAboveMe : boolean = Physics.Raycast(ray, hit);
 
if (Physics.Raycast(ray, hit)) {
    if(hit.collider.name == "Coin")
    {
       goHit = hit.collider.gameObject;
       Destroy(goHit);
    }
}

}

This makes sure the GameObject deleted is always the one raycasted towards and only if the name is Coin!

I would suggest implementing CompareTag instead of searching by name, just for the ease of it!