Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Maypher · Jan 09, 2021 at 04:30 AM · 2d gamerigidbody2dboxcollider2dcomponents

How to enable components after disabling them through script?

I'm trying to replicate the main mechanic of Hue, which, if you haven't played it, let me give you a brief explanation on how it works. Hue is a game where you control colors. You have a pallet of colors which can be used to change the background's color. On every stage there's a puzzle you have to solve. There are some objects that are the same colors as the pallet. If you set the background's color to the same as the obstacle, that obstacle will disappear and if you change to a different color the obstacle will reappear. See image for reference.alt text

I have made a function that changes the background's color depending on what button you pressed.

 public void ChangeColor() 
     {
         string ButtonPressed = EventSystem.current.currentSelectedGameObject.name;
         GameObject button = GameObject.Find(ButtonPressed);
         camera.backgroundColor = button.GetComponent<Image>().color;
     }

And another one that disables the obstacle's Rigibody and BoxCollider depending on what color was chosen.

      public void BoxDetection(string color)
     {
         GameObject[] boxes = GameObject.FindGameObjectsWithTag(color);
         foreach (GameObject box in boxes)
         {
             box.GetComponent<Rigidbody2D>().isKinematic = true;
             box.GetComponent<BoxCollider2D>().enabled = false;
         }
     }

All of this works perfectly fine but my problem is, how do I reactivate the components after the background is changed yet again? Let's say I have a purple box, I change the background to purple and that box "disappears". Now I change the background to green, the purple box will be visible but not interactable. How can I reactivate the components in this situation?

Comment
Add comment · Show 1
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 Llama_w_2Ls · Jan 09, 2021 at 09:23 AM 0
Share

Well if just re-enabling the box's components is what you want, you would search through all gameobjects that have a color tag that is not with the tag 'color' from the function, and just re-enable the box collider and rigidbody. Like so:

          GameObject[] boxes = // Get all gameobjects that aren't with the tag 'color'
          foreach (GameObject box in boxes)
          {
              box.GetComponent<Rigidbody2D>().isKinematic = false;
              box.GetComponent<BoxCollider2D>().enabled = true;
          }

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by BrassChuckle · Jan 09, 2021 at 06:45 PM

I'd use an enum property to track the colors of boxes (call it "ObjectColor" or something). Make a script to use for these boxes, call it "BoxStats", or "Object Stats", or whatever pleases you, and make the enum there. Instead of looking for only boxes of the selected color, cycle through all objects of type "BoxStats" (looks for objects with that script).

For every box that == the selected background color, disable rb and colliders, and do the opposite for all boxes != to the background color.

Here's some sudo-ish code. I've also added a slight optimization; it'll make it so you only have to call "GetComponent" once per box since you'll have direct access to the components via the script.

Maybe have your box/object class be something simple for holding values and references like this:

 public class ColorObject: MonoBehaviour
 {
 
    // define enum
     public enum ObjectColor { Red, Green, Blue}
 
   //declare a variable with your enum type
   //make sure it's public so you can set color in inspector
     public ObjectColor objectColor;

      //you can also cache references to components in your stats script to save having to
      //... GetComponent several times per box, this will save on performance.
     public Rigidbody2D rb2D;
     public BoxCollider2D boxCollider2D;

 
 }


And then modify your current search script just a little to search for the objects with the class type...

 public void BoxDetection(string color)
      {
          ColorObject[] boxes = GameObject.FindObjectsOfType<ColorObject>();
          foreach (ColorObject box in boxes)
          {
              //compare name of enum to color string, if same color then disable
              if(box.objectColor.ToString() == color)
              {
                   //you can eliminate the need to do several GetComponent calls by
                  //... saving references to the ColorObject script, this will save performance cost...
                  //... since get component is quite expensive.
                   box.rb2D.isKinematic = true;                  
                   box.boxCollider2D.enabled = false;
               }
              else
             {
                   //if these boxes are a different color then enable stuff
                   box.rb2D.isKinematic = flase;                  
                   box.boxCollider2D.enabled = true;
             }
              
          }
      }


Comment
Add comment · 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
0

Answer by Bunny83 · Jan 09, 2021 at 09:38 AM

Well, if you have exactly those 8 colors (or other colors but the color count is below say 16) you could simply use collision layers. You give every color a certain layer. When changing the color you also change the layer of the object. Since you want objects with the same color to not collider but all other should collide, you just have to set this up in the collision matrix.


The exact mechanic is not really clear, I never played the game you mentioned. However I would highly recommend that you abstract the "color" away by using a global Color array where you define all your colors. When you want to change the color you would refer to the color by the index. Using single numbers makes your life much simpler in the long run.

Comment
Add comment · 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

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

148 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

Related Questions

Weird 2d collision behaviour 1 Answer

Rigidbody 2D weird physics on kinematic rotate 1 Answer

Drag to add power, release to throw at opposite angle (RigidBody2D), strange beahviour. 3 Answers

How to create a solid object in the canvas? 0 Answers

What's a good way at keeping a ball bounce around inside the scene? 1 Answer


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