code destroys the wrong object

the code destroys a different object then the if/then statement specifys. how to fix?

here’s the code:

`using UnityEngine;

public class Shoot : MonoBehaviour
{
public float range = 100f;
public Rigidbody rb;
public GameObject player;
private GameObject Enemy;

// Start is called before the first frame update
void Start()
{
    Enemy = GameObject.FindWithTag("Target");
}

// Update is called once per frame
void Update()
{
    if (Input.GetButtonDown("Fire1"))
    {
        shoot();
    }
}
void shoot()
{
    RaycastHit hit;
    if (Physics.Raycast(player.transform.position, player.transform.forward, out hit, range))
    {
        if (hit.transform.tag == "Target")
        {
            Destroy(GameObject.FindWithTag("Target"));
        }
        
        

    }
}

}`

Destroy(GameObject.FindWithTag("Target"));

Will destroy the first gameobject it finds with that tag.

You need to take the hit variables gameobject and use that instead.


using UnityEngine;

public class Shoot : MonoBehaviour 
{ 
    public float range = 100f;
    public Rigidbody rb;
    public GameObject player;
    private GameObject Enemy;

    void Start()
    {
        Enemy = GameObject.FindWithTag("Target");
    }

    void Update()
    {
        if (Input.GetButtonDown("Fire1"))
        {
            shoot();
        }
    }
    void shoot()
    {
        if (Physics.Raycast(transform.position, transform.forward, out var hit, range))
        {
            if (hit.transform.tag == Enemy.tag)
            {
                Destroy(hit.collider.gameObject);
            }
        }
    }
}

I assume you want to destroy the object that got hit. The solution is based on that assumption.

Don’t use GameObject.FindWithTag(“Target”) to find game objects to destroy if there are multiple of them sharing the same tag. You won’t find the one that you want. Best case situation for the usability of this function is when you want to find a game object with a tag that’s NOT being shared with another game object. For example, like a manager object.

hit variable contains the information regarding the game object that was hit. You can access it like this.

hit.collider.gameobject

You can use that to destroy the object that was hit.

Sorry initial code is difficult to read

using UnityEngine;

public class Shoot : MonoBehaviour
{
public float range = 100f;
public Rigidbody rb;
public GameObject player;
private GameObject Enemy;

// Start is called before the first frame update
void Start()
{
    Enemy = GameObject.FindWithTag("Target");
}

// Update is called once per frame
void Update()
{
    if (Input.GetButtonDown("Fire1"))
    {
        shoot();
    }
}
void shoot()
{
    RaycastHit hit;
    if (Physics.Raycast(player.transform.position, player.transform.forward, out hit, range))
    {
        if (hit.transform.tag == "Target")
        {
            Destroy(GameObject.FindWithTag("Target"));
        }
        
        

    }
}

}

Thanks, this really helped. it makes sense that the hit variable stores the information about the hit because that’s why it was created! Thanks for helping me!