- Home /
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");
}
}
Answer by robertbu · Jul 21, 2013 at 04:27 AM
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.
Answer by m4s4m0r1 · Jul 20, 2013 at 01:45 PM
simple, just make your prefab`s variable as public variable. for example:
public var Enemy : GameObject;
function MakeEnemy()
{
Instantiate(Enemy, transform.position, transform.rotation);
}
But i want to dewstroy instances on mouse click. I do not want to instatiate code, i know it already. Please read my question once again.
But i want to dewstroy instances on mouse click. I do not want to instatiate code, i know it already. Please read my question once again.
Answer by niraj · Jul 21, 2013 at 07:41 AM
@ robetbu: Thank you so much. It is working for me now. Thank you vey much for your kind help......Have a nice day
Your answer
