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.
This is the pic of gameobject with two children each having circular collider
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.
print("3");
is not showing in the Unity console? – loic.lopez