Parts of code only execute when there is a Debug.Log() statement in them. Why?
Hi.
Throughout my work with Unity I sometimes run into a bug, and a code that isn't executing (I know because a debug log inside doesn't show anything).
To fix I go back in the code and use other Debug.log() statements to get info on my variables, but when I add these suddenly the code that wasn't executed is now executing.
This happens a lot, and I'm at a loss why. Logically maybe the debug.log statement is taking enough time to make the code run correctly, but this indicates a bigger problem. Any help on what I should be looking for when this happens? Thanks.
Here is an example. The editor doesn't show "a" until the previous debug.log() statements are uncommented.
else if (nameOCE != "Floor" && !nameOCE.StartsWith("Wheel") && !otherOCE.CompareTag("bullet"))
{
// Debug.Log(nameOCE);
// Debug.Log(otherOCE.tag);
// Debug.Log(gameObject.layer);
// Debug.Log(otherOCE.gameObject.layer);
if (!otherOCE.CompareTag("Dangerous"))
{
if (otherOCE.CompareTag("future"))
{
if (mymen.misslev == 9)
{
if (!mymen.missdo2)
{
mymen.finmiss(1);
}
}
}
else if (otherOCE.CompareTag("peace"))
{
Debug.Log("a");
if (mymen.misslev == 10)
{
if (!mymen.missdo2)
{
mymen.finmiss(1);
}
}
}
Answer by tanoshimi · Mar 11, 2016 at 01:28 PM
There's no reason why the inclusion of Debug.Log would make a difference, unless the tiniest timing delay of printing to the console had meant that the value of one of the conditions had changed (which seems incredibly unlikely). However, your code has several logical inconsistencies and redundancy. I'd set about correcting those and see if it makes a difference:
As far as I can tell, the entire logic can be reduced to:
else if (nameOCE != "Floor" && !nameOCE.StartsWith("Wheel") && !mymen.missdo2) {
if (otherOCE.CompareTag("future") && mymen.misslev == 9) {
mymen.finmiss(1);
}
else if (otherOCE.CompareTag("peace") && mymen.misslev == 10) {
mymen.finmiss(1);
}
}
How is this being called? In an OnCollisionEnter? It might help to see the whole function.
Thank you for the answer. Yes the code is too long for its own good, but I separated the if statements because it's easier to organize for me since I added a Debug.Log() in every step of the way as some conditions weren't firing (and the code continues beyond the snippet posted, taking into account the tag being "Dangerous").
Indeed it is in OnCollisionEnter. What does that tell you?
Answer by shlingg · Jul 17, 2016 at 10:24 PM
Did you find a solution to this problem? I'm experiencing the same thing with if statements and while loops!
Your answer
Follow this Question
Related Questions
The variables modified in the inspector are not kept on build 0 Answers
Custom inspector. Pick one bool from a list. 1 Answer
After Build Scripts no longer functioning in build or editor 1 Answer
Setting GameObject properties in editor when public script fields are changed 2 Answers
PostProcessingFactory.cs 2 Answers