Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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, 2019 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, 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
     }
 }


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, 2019 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, 2019 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, 2019 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, 2019 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.

Follow this Question

Answers Answers and Comments

169 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

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

How to check if player is stuck inside of ground 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges