make sure there is no overlap when re-enabling objects
I am disabling objects when shot, but the problem is I don't know how to check if there's something is in the way (player or enemies etc) when re-enabling the objects. Right now the objects get re-enabled even if the player is in the way and that's causing issues. Here's my code:
public class BreakingBlocks : MonoBehaviour
{
public float DestroyTimer = 0F; /// Wait time before destoying
public float RespawnTimer = 1.5F; /// wait time before respawning
public bool DisableRespawning = false; /// Disable respawning
void OnTriggerEnter2D(Collider2D collider)
{
if (collider.gameObject.tag == "BreakBlocks")
{
Invoke("RandomCube", DestroyTimer);
}
}
void RandomCube()
{
gameObject.SetActive(false);
if (DisableRespawning)
{
return;
}
Invoke("SpawnCube", RespawnTimer);
}
void SpawnCube() // re-enable objects
{
gameObject.SetActive(true);
}
Answer by dan_wipf · Mar 31, 2019 at 04:28 PM
i’d change the gameobjects tag before disabling, then on setactive you first check if the collider intersects with anything, like player or what ever you dont want to be overlapping with. => for example a bunch of grass is ok to be overlapped with, but not a player/enemy so after you setactive(true) you can check with this:
GameObject[] AllCharacters;
bool intersects = false;
var bb = GetComponent<Collider>().bounds;
for(int i = 0; i<AllCharacters.Length;i++){
if(bb.Intersects(AllCharacters[i].GetComponent<Collider>().bounds)){
intersects = true;
return;
}
}
if(intersects){
//do something
intersects = false;
}
// Second Solution
void OnCollisionStay/OnTriggerStay(Collision / Collider col){
if(col.transform.tag == "Player" || col.transform.tag == "Enemy"){
//do something
}
}
Getting issues: GameObject[] does not contain a definition for 'GetComponent' and no accessible extension method 'GetComponent' accepting a first argument of type GameObject[] could be found (are you missing a using directive or an assembly reference?) I did change Collider to Collider2D.
my bad should be AllCharacters[i].GetComponent...
but anyway it’s pseudo code, just to get you an idea
ok thanks, I'm new so I haven't used arrays before but this helps.
just keep asking, if anything is unclear => i’d say the first method can be a heavy method of comparing BB’s against each other, like if you have hundreds of enemys!
if you wan’t to add all enemys to a Array do this => add a tag to the enemy => “enemy”
if the enemys’ are not spawning through the game you can add this to the Start method, elses you need to catch all enemys before checking the place.
AllCharacters = GameObject.FindGameObjectsWithTag("enemys");
working with multiple types of gameobjects, you’re better of with List where you can enter code here
easily add Ranges, like this:
AllCharacters.AddRange(GameObject.FindGameObjectsWithTag("enemys"));
//or single Objects
AllCharacters.Add(GameObject.FindGameObjectsWithTag("Player"));
Your answer

Follow this Question
Related Questions
Destroying a projectile on collision 1 Answer
OnCollisionEnter2D message being ignored 1 Answer
Can i find which script destroy my GO? 1 Answer
Trouble with earlier triggered 0 Answers