Unity: Script won’t delete 1 of the objects if 2 are colliding

I have this C# segment of code to make auto generated rooms, does anyone know why my code won’t delete the clones of the object? I’ve tried a lot of methods, but I can’t for the life of me get it to work. The project is in 3D.



{
    [SerializeField] private int intRandomValue;
    public float targetTime = 5.0f;

    public Vector3 SpawnPos;
    //public testscore script;

    void Start()
    {
        SpawnPos = new Vector3(0, 0, 0);


    }

    void FixedUpdate()
    {
        
        intRandomValue = Random.Range(0, 6);
        if (intRandomValue == 1)
            Instantiate(Cube, SpawnPos, Quaternion.identity);
        if (intRandomValue == 1)
            tag = "NewRoom";
        if (intRandomValue == 1)
            SpawnPos += new Vector3(0, 5, 0);
        if (intRandomValue == 1)
            tag = "Room";



        if (intRandomValue == 2)
            Instantiate(Cube, SpawnPos, Quaternion.identity);
        if (intRandomValue == 2)
            tag = "NewRoom";
        if (intRandomValue == 2)
            SpawnPos += new Vector3(0, -5, 0);
        if (intRandomValue == 2)
            tag = "Room";
       
        
        if (intRandomValue == 3)
            Instantiate(Cube, SpawnPos, Quaternion.identity);
        if (intRandomValue == 3)
            tag = "NewRoom";
        if (intRandomValue == 3)
            SpawnPos += new Vector3(5, 0, 0);
        if (intRandomValue == 3)
            tag = "Room";
       
        
        if (intRandomValue == 4)
            Instantiate(Cube, SpawnPos, Quaternion.identity);
        if (intRandomValue == 4)
            tag = "NewRoom";
        if (intRandomValue == 4)
            SpawnPos += new Vector3(-5, 0, 0);
        if (intRandomValue == 4)
            tag = "Room";
       
        
        if (intRandomValue == 5)
            Instantiate(Cube, SpawnPos, Quaternion.identity);
        if (intRandomValue == 5)
            tag = "NewRoom";
        if (intRandomValue == 5)
            SpawnPos += new Vector3(0, 0, 5);
        if (intRandomValue == 5)
            tag = "Room";
       
        
        if (intRandomValue == 6)
            Instantiate(Cube, SpawnPos, Quaternion.identity);
        if (intRandomValue == 6)
            tag = "NewRoom";
        if (intRandomValue == 6)
            SpawnPos += new Vector3(0, 0, -5);
        if (intRandomValue == 6)
            tag = "Room";
    }

    void OnTriggerEnter(Collider other)
    {
        if (CompareTag("NewRoom"))
        {
            Destroy(gameObject);
        }
    }
}

please help thanks
I don’t really know anymore details I can add srry
erm uh filler content here bc word minimum

New contributor

Gleep Glorp is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

2

A few things:

  1. tag = "Room"; sets the tag of the GameObject that is spawning the child objects, not the child object themselves. To be able to access this new child, we need to keep a reference to it, meaning that we need the if statements to contain more than one line. It should instead be something like:
if (intRandomValue == 1)
{
    GameObject cube = Instantiate(Cube, SpawnPos, Quaternion.identity);
    cube.tag = "NewRoom";
    SpawnPos += new Vector3(0, 5, 0);
    cube.tag = "Room";
}
  1. Now that we’ve put the logic into a single if statement, you can see how every cube will end up with the tag “Room”, since you overwrite it at the end. You should only set the tag one time, with the proper value.

  2. The OnTriggerEnter‘s CompareTag call checks against the tag of the current (spawner) object, not the object you’ve collided with. It’ll also delete the spawner object, not the room. You’ll instead want to check:

void OnTriggerEnter(Collider other)
{
    if (other.CompareTag("NewRoom"))
    {
        Destroy(other.gameObject);
    }
}
  1. OnTriggerEnter will only get called if the spawner object has a (non-trigger) RigidBody and collides with another object that has a trigger collider. I doubt that your room spawner has a RigidBody, what did you actually want to achieve with this OnTriggerEnter callback?

1

you need to fix your OnTriggerEnter function:

 void OnTriggerEnter(Collider other)
    {
        if (other.CompareTag("NewRoom"))
        {
            Destroy(other.gameObject);
        }
    }

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật