Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 rogaroga · Dec 14, 2016 at 06:47 AM · syntax

Is it wrong to leave an if statement empty in order to use else?

I'm fairly new to this, so I apologize if this is a really dumb question: Is it OK to do this with an if/else combination, or is this a coding no-no? Specifically, the "do nothing" portion of it in order to nest another if/else in the else:

 //If stars left to collect, collect1UI is visable, otherwise it is not.
         if (star1off == true) {
             //do nothing.
         }else{
             if (collectOne - collectedOne >= 1) {
                 var go = GameObject.Find ("Collect1UI");
                 go.SetActive (true);
             } else {
                 var go = GameObject.Find ("Collect1UI");
                 go.SetActive (false);
                 star1off = true;
                 starOneName = "empty";
             }
         }        
Comment
Add comment · Show 4
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 rogaroga · Dec 14, 2016 at 06:48 AM 0
Share

By the way, the code works, I'm just trying to find out if it is really bad to do things this way. Thanks! :)

avatar image Dave29483 · Dec 14, 2016 at 08:06 AM 1
Share

Why not use if (star1off == false) ?

Negate the need for the empty section in the first place?

avatar image rogaroga · Dec 14, 2016 at 08:56 AM 0
Share

Dave, I don't have a good answer. I've changed it to that, and it works the same, so thanks. Cleaner for sure. Was just curious if having an empty statement like that would cause problems. Trying to learn, but I guess I can't think of a case in which the logic can't be flipped to eli$$anonymous$$ate the "else" and just have the "if", so I guess it doesn't matter :)

avatar image Dave29483 · Dec 14, 2016 at 08:59 AM 0
Share

It won't cause issues and will likely be optimized away by the compiler. So I guess it's more about good practice :)

2 Replies

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

Answer by miguellahoz · Dec 15, 2016 at 05:28 PM

You can always do:

 if (!star1off) {
             if (collectOne - collectedOne >= 1) {
                  var go = GameObject.Find ("Collect1UI");
                  go.SetActive (true);
              } else {
                  var go = GameObject.Find ("Collect1UI");
                  go.SetActive (false);
                  star1off = true;
                  starOneName = "empty";
              }
          }   

The '!' just negates the star1off, so if it's true, !star1off will be false.

It's better to not leave an empty 'if' just to use an else statement, but if you understand better the if statement leaving it empty, go ahead!

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
1

Answer by JeffHardddyyy · Dec 14, 2016 at 02:10 PM

I probably should put this in a comment, but I probably won't recognize it in my email unless I make it in my own answer.

Is it wrong ? Is it weird ?

No, the answer to both of them is no. As long as you are happy with it and it has no errors, nothing should stop you with it. I mean who knows, you might be able to include something in there sometime.

But for right now, if you're happy with it, just leave it. I mean I get what other people will say with "No" and "Its weird and unorganized so don't do it" and my reply to that is : If you know where everything is, then that's arguments are kinda invalid.

I alone usually use alot of empty if's statement for no reason, I'm not home at the moment but if you reply to this answer asking, I will show you.

In addition I saw you added that you are new so lemme just say: Welcome ! :) and good luck in your projects

Comment
Add comment · Show 1 · 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 rogaroga · Dec 14, 2016 at 09:44 PM 1
Share

Thanks!!!!

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

87 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

Related Questions

how can I have a game object get a variable from a different object with collision 1 Answer

Instantiating Enemies for RPG 0 Answers

What is the purpose of the number sign within Unity-official scripts? 1 Answer

When i type code to move my person i get errors 1 Answer

,cant access to another class variavble 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