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";
}
}
By the way, the code works, I'm just trying to find out if it is really bad to do things this way. Thanks! :)
Why not use if (star1off == false) ?
Negate the need for the empty section in the first place?
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 :)
It won't cause issues and will likely be optimized away by the compiler. So I guess it's more about good practice :)
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!
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
Your answer
Follow this Question
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