Search Unity

[Unity3] No more short-circuiting?

Discussion in 'Editor & General Support' started by StarManta, Aug 7, 2010.

  1. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    I just upgraded a project to 3.0, and one of the errors I'm getting is this:
    Code (csharp):
    1.  
    2. MissingComponentException: There is no 'Renderer' attached to the "Muzzleflash" game object, but a script is trying to access it.
    3.  
    Looking into it, the problematic line turned out to be:
    Code (csharp):
    1.  
    2.  
    3.     if (alwaysUpdateIfVisible  renderer  renderer.enabled) {
    4.  
    Now, the way it used to work is that Unity would get to the 'renderer' clause, it would resolve to 'false', and because there's no way the next part of the statement could change the final result of the if statement, Unity wouldn't execute it at all, making this a handy trick for conveniently making sure Unity doesn't execute code on non-existing components. Does if statement short-circuiting behave differently now?

    I've broken the statement up to check, and both of these return false as expected:
    Code (csharp):
    1.  
    2.  
    3.     if (renderer) {
    4.     if (renderer != null){
    5.  
    So it does appear that, despite the renderer not existing, the code goes ahead and executes the last bit of the if statement anyway.