B
B
Bruh_Bruh2020-10-04 09:27:37
Unity
Bruh_Bruh, 2020-10-04 09:27:37

How to implement the removal of one of the many identical objects?

Hello, I'm making a shooter. I need to implement the killing of an object, this is not a problem for me, but only when it is alone. and so I have 10 of them. I shoot at them and when the enemy’s health is over, he must die - retire, but how to delete exactly the one I shot at, because they all have the same tag, etc., since they spawn from the prefab ? I tried to implement this theme, but it gives an error -
"Destroying assets is not permitted to avoid data loss.
If you really want to remove an asset use DestroyImmediate (theObject, true);
UnityEngine.Object:Destroy(Object)
Shooting:Update() ( at Assets/Scripts/Shooting.cs:28)"

public class Shooting : MonoBehaviour
{
    public Camera cam;
    public float RayLength;
    public Enemy enemy;
    public GameObject EnemyBody;
    public float damage = 25f;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetButton("Fire1"))
        {
            Shoot();
        }

        if (enemy.Health <= 0)
        {
            Destroy(EnemyBody);
        }
    }

    void Shoot()
    {
        RaycastHit hit;

        if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, RayLength))
        {

            if (hit.rigidbody.CompareTag("Enemy") && hit.rigidbody != null)
            {
                enemy.TakeDamage(damage);
            }
        }
    }

public class Enemy : MonoBehaviour
{
    private Rigidbody rb;
    public float Health;
    public GameObject Player;
    public EnemySpawn enemySpawn;
    // Start is called before the first frame update
    void Start()
    {
        rb = GetComponent<Rigidbody>();
        Health = 100f;
        Player = GameObject.FindGameObjectWithTag("Player");
    }

    // Update is called once per frame
    void Update()
    {
        rb.transform.position = Vector3.MoveTowards(transform.position, Player.transform.position, 5f * Time.deltaTime);
    }

    public void TakeDamage(float amount)
    {
        Health -= amount;
        if (Health <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        Destroy(gameObject);
    }


public class EnemySpawn : MonoBehaviour
{
    public GameObject Enemy;
    public int enemyCount;
    // Start is called before the first frame update
    void Start()
    {
        StartCoroutine(da());
        Enemy = GameObject.FindGameObjectWithTag("Enemy");
    }

    // Update is called once per frame
    void Update()
    {
        
    }

    public IEnumerator da()
    {
        while (enemyCount < 10)
        {
            Instantiate(Enemy, new Vector3(Random.Range(-100, 100), 1f, Random.Range(-100, 100)), Quaternion.identity);
            yield return new WaitForSeconds(3f);
            enemyCount += 1;
        }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
ReWire_92, 2020-10-04
@ReWire_92

Why are you doing Destroy in the Shooting class if you already have a Die function in the Enemy class that destroys the gameobject?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question