Switch scenario throws illogical "BCW0015: WARNING: Unreachable code detected" Error

Hey, i have been making a un-guided rocket script and have encountered an odd error during ray-casting for collision detection. The ray-cast works perfectly well, but when I try to switch the RayCastHit.collider.tag, Unity Editor throws an inaccessible code error after compiling. I have traced all the code in the script dozens of times over so please dont ask to see more code, i guarantee you, there is nothing else wrong, the function is simply called in an if statement on update and IT WAS WORKING until a day ago. I dont get why I cant “switch(myRaycastHit.collider.tag)”. as far as I can see its a problem with Unity’s compiler or the editor. Any ideas what to do?

//Execute collision side-effects
function CollisionHandler(): boolean
{
    //Update next position
    UpdatePos();
    //Extrapolative raycast collision check
    if(Physics.Raycast(transform.position, targetDir, hitInfo, targetDir.magnitude))
    {
        switch(hitInfo.collider.tag)
        {
            case "NonPhysicalCollider":
            case "Player":
         
                return true;
             
            default:
         
                hitInfo.transform.SendMessage("Hit", hitDamage, SendMessageOptions.DontRequireReceiver);
                transform.position = hitInfo.point - (targetDir.normalized / 10);
                emitter.Emit();
                return false;
        }
    }
    else
    {
        return true;
    }
}

Edit 1: error occurs on Line 9 of the code

First of all, it’s a warning not an error; you can ignore it.

Second, you DO have unreachable code. Neither of the “break” lines in there will ever be executed. Just get rid of them if you want to get rid of the warning.

I know its a warning, im not a tool! but its causing a fault at collision detection. How on earth wont the break functions be called?

Edit: my bad, return XD thanks

The code is not being executed within the switch scenario. it never enters the switch.

because you return before you break… so you code exits the function before either break is called.

Even with the break statements removed, i still get an inaccessible code warning

…defensive much? Don’t take things so personally, damn. But you do refer to this as an “error” twice in your post.

This wouldn’t be causing any real problems. Your issue is probably somewhere else.

They’re after the “return” lines. The instant you return something (with the exception of “yield return”), the function ends. There’s no way it will ever reach those lines.

yea, yea im aware of that, the lines were left in during messing around trying to get the code to work. EVEN WITH THE LINES REMOVED, there is still an inaccessible code error

sorry for referring to it as an error, i also called a break a function. im tired. but the break lines are not the problem. I have pointed out to you that the switch case scenario is never entered, I have tested this. i wish i never accidentally left the break lines in, but once again im tired.

So is it always going to the “else” part of it then? If so, then your raycast is where the problem is.

As mentioned in this thread, try the solution of setting a temp value in your case blocks, then returning at the end of the function.

1 Like

Now we’re on the same page… the raycast works, I am intending the default commands to be executed 99% of the time, the first two tags(Player and NonPhysicalCollider) are meant to be ignored by the missile. i can print any of the rayCastHit stuff to the debug line, and it has correct values. but the second I type switch(anyGameObjectOrComponent.tag) unity gives an inaccessible code warning

YOU ARE GOD! thanks sosososososososososoooooooo much