- Home /
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")); } } } }
Sorry initial code is difficult to read
using UnityEngine;
public class Shoot : $$anonymous$$onoBehaviour { 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"));
}
}
}
}
Can you edit the original post with the correct formatting please. I will try to answer it whilst you are.
Answer by sacredgeometry · Jan 05, 2020 at 11:05 PM
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);
}
}
}
}
Answer by IINovaII · Jan 05, 2020 at 11:11 PM
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.
Answer by spmacivor · Jan 06, 2020 at 02:34 PM
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!