• 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 $$anonymous$$ · Jul 07, 2018 at 10:53 AM · moveif-statementscompiler

2D Hiding mechanic troubles (Else statement not executing)

So I am trying to make a hiding mechanic, and this method called hide is called when you press e, but it does not work fully.

So when I press E the character vanishes, this is supposed to happen, then I press E again, but nothing happens, at one point I put a debug.log into the else statement, and I found that it never got put onto the console.

 void hide()
     {
         if (Player.GetComponent<BoxCollider2D>().enabled == true)
         {
 
             Player.GetComponent<BoxCollider2D>().enabled = false;
             Player.GetComponent<Rigidbody2D>().simulated = false;
             Player.GetComponent<SpriteRenderer>().enabled = false;
 
         }
         else 
         {
             Player.GetComponent<BoxCollider2D>().enabled = true;
             Player.GetComponent<Rigidbody2D>().simulated = true;
             Player.GetComponent<SpriteRenderer>().enabled = true;
           
         }
     }


anyone know whats going on here?

Comment
Add comment · Show 6
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 JVene · Jul 07, 2018 at 04:33 PM 0
Share

No way to see all, but is this without any doubt the same Player, AND the same BoxCollider2d?

Verify by setting a breakpoint at the start of hide when you expect enabled to be false, and inspect the values.

Come back with the results of that still puzzles.

avatar image $$anonymous$$ JVene · Jul 10, 2018 at 02:17 PM 0
Share

I put in the breakpoint and found even that was not activated. No clue why, note that my code at this point looked like this, as I had tried some other things:

   void hide()
 
     {
 
         if (Status == 0)
         {
 
             Status = 1;
             Player.GetComponent<SpriteRenderer>().enabled = false;
             Player.GetComponent<BoxCollider2D>().enabled = false;
             Player.GetComponent<Rigidbody2D>().simulated = false;
 
             Rigi = false;
             Spi = false;
             Box = false;
 
         } //break point was never activated, so the else statement as a whole simply is not executing it seems.
 
          else 
         {
             Status = 0;
             Player.GetComponent<Rigidbody2D>().simulated = true;
            Player.GetComponent<SpriteRenderer>().enabled = true;
            Player.GetComponent<BoxCollider2D>().enabled = true;
 
             Rigi = true;
             Spi = true;
             Box = true;
         }
     }


So I put a breakpoint in the if statement, and found the variable status was 0, which was odd considering by the point the if statement breakpoint was activiated, I am fairly sure the number should've been a 1...

avatar image JVene $$anonymous$$ · Jul 10, 2018 at 05:40 PM 0
Share

Next up, you need to find anywhere else Status might be changed to a zero behind your back ;)

What class owns the hide function?

Show more comments

1 Reply

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

Answer by $$anonymous$$ · Jul 11, 2018 at 02:53 PM

Okay, after recieving some help (Comments to my post here) those who were helping me sort this out came to this conclusion: Use OnTriggerEnter and Exit and not Stay. This seems to be working.

 private int Status; //0 = nohide, 1 = hide
 
     private void Start()
     {
         Status = 0;
 
        
     }
 
     public GameObject Player;
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         hide();
     }
 
     private void OnTriggerExit2D (Collider2D other)
     {
         hide();
     }
 
     private bool Rigi; 
     private bool Spi; 
     private bool Box; 
 
     void hide()
 
     {
 
         if (Status == 0)
         {
 
             Status = 1;
             Player.GetComponent<SpriteRenderer>().enabled = false;
 
     
 
           
             Spi = false;
             Box = false;
 
         } //break point was never activated, so the else statement as a whole simply is not executing it seems.
 
          else 
         {
             Status = 0;
 
            Player.GetComponent<SpriteRenderer>().enabled = true;
           
 
           
             Spi = true;
             Box = 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

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

88 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

Related Questions

NullReferenceException: Object reference not set to an instance of an object 1 Answer

How can I change a scene by the rotation z position?, 1 Answer

Why my function not working with less than velocity?,why my function not work with an If statements with a greater/less than 1 Answer

How to move an object from point A to point B with one key press 3 Answers

Moving a Rocket problems 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