- Home /
Exiting an if statement when calling a function from another script (Possible bug)
Hi, in my code I have an If statement that's conditional is based on the currentBaseState of my mecanim controller and currently the statement returns when it reaches a call to a separate script?
if (currentBaseState.fullPathHash == hashes.attackState)
{
Target.GetComponent<EnemyScript>().GetHit(damage);
impacted = true;
runAttacked = false;
Debug.Log("Boom");
}
The Target is a transform for the enemy and it calls the enemy script to take damage. The impacted and runAttacked are just bools and the Debug.Log("Boom") is just to register the conditional in my console.
Is this a bug or am I not understanding how an if statement works with calling another function from a separate script?
Please help!
So the problem is the 'boom' doesn't log to console ?
It will only return if there's an exception like a null ref exception in that mehod calling line.
No, the bools after dont get set either... It works just fine if the GetComponent line is commented out...
This doesnt make sense
Sorry Nose$$anonymous$$ills, you were correct as well
Answer by Bunny83 · Jun 29, 2016 at 02:39 PM
Well, if the booleans are set and the Debug.Log shows up when the line is commented out, but aren't executed when it's not that means that line throws an exception. However as Yash said in the comment above you wouldn't notice this exception if you catch it manually with try catch or if it's executed inside a coroutine. If you don't you should see the exception in the console.
All that information is missing in your question. So you should provide more context. Where is that snippet of code located.
Ah wow, thanks Bunny83, so the problem was I had no attached the Enemyscript to GameObject that I was attacking. Once I did that it worked great.
Also, I made the mistake of collapsing the Errors in the console so I was only seeing the Logged comments.