cant destroy other object

So I have created a character that can shoot and some things to shoot at, and that all works well and good. However, I want the bullets to destroy the objects they collide with (they happen to be clones from another object that are being launched) and I have no Idea why my code isn’t working. Any help?

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class enemyhp : MonoBehaviour {

// Use this for initialization
void Start () {
	
}

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

private void OnTriggerEnter(Collider other)
{

    if (other.gameObject.tag == "enemy")
    {
        Destroy(other);
        Debug.Log("Collided");
    }
}

}

1 Answer

1

Should be Destroy(other.gameObject)

Else it will just destroy the Collider of “other”

thanks, that was it.

–

You can make carvar an array and in OnTriggerEnter use carvar += [other.gameObject]; if you want to limit it you can put that line under if(carvar.Length < numberOfCarvarAllowed) You can then get the boxcollider width by using other from the trigger or carvar[1] e.g. carvar[1].GetComponent<BoxCollider>().size.x

–