GameObjects

So I have three GameObjects within the scene with the same name (“Player”), while I’m shooting one GameObject, the other GameObject is destroyed. How can I have the same GameObject I am shooting at destroyed. The code is:

public Vector3 fire;

var player1 = GameObject.Find("Player");
var playerloss = GameObject.Find("Player").GetComponent<Player_health>();

RaycastHit hit;

fire = hit.point;
playerloss.health -= damage;

GameObject.Find finds a GameObject by name and returns it. If you have multiple gameObjects named Player, you might not retrieve the one which has been hit.

Instead, retrieve the component on the hit object. Since you haven’t provided your script, the following lines of code must be adapted to your situation

RaycastHit hit;
if (Physics.Raycast( transform.position, transform.forward, out hit) )
{
     fire = hit.point;
     Player_health hitPlayer = hit.transform.GetComponentInChildren<Player_health>();
     if( hitPlayer != null )
         hitPlayer.health -= damage;
     else
         Debug.LogWarning("No player hit");
}