0
votes

I have attached a script to a prefab and the script is:

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

public class destroyer: MonoBehaviour {

    Circles circles;

    Collider2D collider1;
    Collider2D collider2;

    private void Start() {
        collider1 = gameObject.transform.GetChild(0).GetComponent < Collider2D > ();
        collider2 = gameObject.transform.GetChild(1).GetComponent < Collider2D > ();
        circles = FindObjectOfType < Circles > ();
    }

    private void Update() {
        if (transform.position.y < 2) {
            Destroy(gameObject);
            circles.instantiator();

        }

    }
    void OnTriggerStay2D(Collider2D other) {

        if (collider1.bounds.Contains(other.bounds.max) && collider1.bounds.Contains(other.bounds.min)) {
            print("2");
            if (other.bounds.Contains(collider2.bounds.max) && other.bounds.Contains(collider2.bounds.min)) {
                print("3");
                if (transform.position.y > 3) {
                    print("4");
                    Destroy(other.gameObject);
                    Destroy(gameObject);
                    circles.instantiator();
                }

            }

        }
    }

}

OnTriggerStay2D function is getting called but the condition when a GameObject is completely inside the collider 1 and outside the collider 2 is never true even if the other GameObject satisfies the condition. Previously when I have attached both collider to the prefab then this condition becomes true when the game object satisfy the condition. But after the change it is not working. I think that I am not accessing the collider of a child properly or there is some other error. Please help me to resolve this problem.

enter image description here

This is the pic of gameobject with two children each having circular collider

enter image description here

and when other game object having circular collider comes in between both the circular collider then I want to perform some action. this is ultimately what I want to achieve

Edit : the same function is working fine and I am getting what I want when I put the prefab on the scene and then running the game . But it is not working with the same prefab which is instantiated after running the game.

1
I think you may create scripts next to your children colliders and separate your logic from each collider.loic.lopez
@loic.lopez you are saying to attach two different script to both the child and then perform the function? But I want to know that why this piece of code is not running even when the condition is true. How can I make it work ?Mohammad AL - Haque
If I understand well the print("3"); is not showing in the Unity console?loic.lopez
@loic.lopez print("2") is also not showing in the unity consoleMohammad AL - Haque
@MohammadAL-Haque the two colliders are enabled and active right?derHugo

1 Answers

0
votes

I think you can improve your conditions by using Collider2D.IsTouching like so:

void OnTriggerStay2D(Collider2D other) {

    if (collider1.IsTouching(other)) {
        print("2");
        if (other.IsTouching(collider2)) {
            print("3");
            if (transform.position.y > 3) {
                print("4");
                Destroy(other.gameObject);
                Destroy(gameObject);
                circles.instantiator();
            }

        }

    }
}

Reference: ScriptReference/Collider2D.IsTouching

Check whether this collider is touching the collider or not.
It is important to understand that checking whether colliders are touching or not is performed against the last physics system update; that is the state of touching colliders at that time. If you have just added a new Collider2D or have moved a Collider2D but a physics update has not yet taken place then the colliders will not be shown as touching. This function returns the same collision results as the physics collision or trigger callbacks.

EDIT 1:

You can also combine the previous code with: Physics2D.OverlapCollider

So you can do:

void OnTriggerStay2D(Collider2D other) {

    if (collider1.IsTouching(other)) {
        Collider2D[] results;
        int elements = collider2.OverlapCollider(new ContactFilter2D, results);
        if (elements > 0) {
            // do some extra work with your result Collider2D array
            // here
            //
            if (transform.position.y > 3) {
                print("4");
                Destroy(other.gameObject);
                Destroy(gameObject);
                circles.instantiator();
            }

        }

    }
}