• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Legionghost · Mar 31 at 01:09 PM · collision2ddisable object

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);
     }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by dan_wipf · Mar 31 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
     }
 }


Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Legionghost · Mar 31 at 11:17 PM 0
Share

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.

avatar image dan_wipf Legionghost · Apr 01 at 03:49 AM 0
Share

my bad should be AllCharacters[i].GetComponent...


but anyway it’s pseudo code, just to get you an idea

avatar image Legionghost · Apr 01 at 04:55 AM 0
Share

ok thanks, I'm new so I haven't used arrays before but this helps.

avatar image dan_wipf Legionghost · Apr 01 at 05:39 AM 0
Share

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 hereeasily add Ranges, like this:


 AllCharacters.AddRange(GameObject.FindGameObjectsWithTag("enemys"));
 //or single Objects
 AllCharacters.Add(GameObject.FindGameObjectsWithTag("Player"));
 



Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

173 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Having trouble disabling and enabling game objects 0 Answers

What is with the colon? 1 Answer

Player not dying in apk but does in unity 0 Answers

Collision issue between layers 0 Answers

2D collision Pipes to act like 3d (channels / layers) 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges