How to Destroy Individual Instance of an Object

Hi friends,

I am doing a testing where multiple instances of the game object creating in the scene with tag Name=“enemy”. I want to click them and destroy individually.

I have two game objects:

  • circleObject(whose instances are
    creating in the scene regularly with
    the attached EnemyScript.js)
  • playerObject(who is raycasting and
    finding which object got hit by ray
    by their Tag Name “enemy” with the attched playerScipt.js)

Now i want when the ray hits the any instance of the object circleObject that particular instance got desroyed keeping other instances remain intact.

What to include in playerSctipt.js to destroy particular instances? Please Help.

Note: All instances having tag name “enemy”

My playerScript.js :

// Script for Player

var hit : RaycastHit;

var ray : Ray = Camera.main.ScreenPointToRay(Input.mousePosition); // Get mouse position and point a ray from there

	//casts a ray against all colliders in the scene
	
	if(Physics.Raycast(ray, hit, rayDistance))
	{
		if(hit.transform.tag == tagName)
		{
		
		  print("Hitting");
		
		}
		else
		{

		  print("This is not an enemy");

		}
	}

simple, just make your prefab`s variable as public variable. for example:

public var Enemy : GameObject;

function MakeEnemy()
{
  Instantiate(Enemy, transform.position, transform.rotation);
}

On line 7, put:

Destroy(hit.collider.gameObject);

This will destroy the specific game object hit by the Raycast(). You could also use hit.transform.gameObject.

@ robetbu: Thank you so much. It is working for me now. Thank you vey much for your kind help…Have a nice day