Unreachable code detected.

I keep getting this error message what does it mean?

Hey this is my code

static var stringToEdit = "Hello World"; var area: int = 100; var target: Transform; var AngleDistance = 10.0; var attackRange = 100.0;

function Start () { if (target == null && GameObject.FindWithTag("First Person Controller")){ target = GameObject.FindWithTag("First Person Controller").transform; stringToEdit=("A Player was found you are the enemy"); } }

function Update (){ if (target ==null) return;

if (!CanSeeTarget ()) return;

var fwd =(transform.TransformDirection (Vector3. right));

if (Physics.Raycast (transform.position, fwd, area)){

stringToEdit=("A Bullet was shot");

}

}

function CanSeeTarget (): boolean { if (Vector3.Distance(transform.position, target.position) > attackRange) return false; stringToEdit=("Player is out of range "); var hit : RaycastHit; if (Physics.Linecast (transform.position, target.position, hit)) return hit.transform == target;

return false; stringToEdit=("linecast seen"); }

From what you are saying, I think its the last line that is the problem.

Updated Answer

Hey logingeneralgadd. You are correct. Based on the code sample you posted, the last line is the problem.

The logic on the last lines of your CanSeeTarget function reads: "if the linecast hits, return whether the hit's transform equals the target, otherwise, return false. The program stops reading after that and never sees your last line, hence the error.

function CanSeeTarget (): boolean { 
    if (Vector3.Distance(transform.position, target.position) > attackRange)
        return false; 

    stringToEdit=("Player is out of range "); 
    var hit : RaycastHit; 

    if (Physics.Linecast (transform.position, target.position, hit))
        return hit.transform == target;

    return false; 

    stringToEdit=("linecast seen"); 
}

You can easily fix this in one of two ways. Use a return variable:

var result = false;
if (Physics.Linecast (transform.position, target.position, hit))
    result = hit.transform == target;

stringToEdit=("linecast seen");
return result;

Or add your last line to both return statements:

if (Physics.Linecast (transform.position, target.position, hit)) {
    stringToEdit=("linecast seen");
    return hit.transform == target;
}

stringToEdit=("linecast seen");
return false; 

Original Answer

It means that you have created code that cannot possibly be called. Usually this is due to code being placed in a function in places that will never execute such as this:

void TestFunction()
{
    const bool isTrue = true;
    if(isTrue)
    {
        Debug.Log("Its True!!!");
    }
    else
    {
        Debug.Log("Its False!!!");
    }
}

The compiler will see that the code in the else statement will never run because isTrue can't be anything other than true the way the code is written.

Alternatively:

bool TestFunction2()
{
    try
    {
        //Do some cool stuff

        UpdateSpacecraftPosition();

        ShowSuperCoolExplosions();

        return true;//Success!
    }
    catch(Exception ex)
    {
        //Handle the exception
        return false;//Failure!!!
    }

    //Perform cleanup
    DestroyExplosionFragments();
}

The DestroyExplosionFragments function will never be called because you will return true or false before getting there, thus ending the function.

If you want, edit your question and post the function code and I can help you track down the unreachable code. This problem should always be resolved 1) to keep your code clean so it doesn't confuse you months from now and 2) so that if you have critical code that you think is being run, but actually isn't you can fix that.

sometimes when you write code, some lines of code are not reachable because there is a return statement is before them that always execute or because they are in a conditional statement or loop that never execute.