- Home /
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.
Hi logingeneralgadd, I updated my answer based on the code you uploaded
Answer by Michael La Voie · Mar 05, 2010 at 08:05 PM
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.
I edited your code slightly..."bool isTrue = true" won't cause "Unreachable code detected", but making it a constant does.
@$$anonymous$$ichael I would suggest to always use the brackets {} even in a single line if-statements.. if we forget about the fact that it's bad practice.. it's quite confusing for new programmers and it's a source of many errors.. for example if they want to add something the most natural way will be adding a line..! No build errors or anything.. but the result is wrong! :)
Hey Lipis, although I would have to disagree that it is inherently wrong I do agree with you and always use curly braces in my personal code. I only omitted them when referring to @logingeneralgadd's code as it omitted them.
Answer by Ashkan_gc · Mar 05, 2010 at 09:01 PM
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.
Your answer
Follow this Question
Related Questions
Error When Assigning A GameObject To Variable Inside A Class 1 Answer
cant install unity 1 Answer
Unexpected symbol 'Flip' and 'else' 1 Answer
Android build problem! 1 Answer
Sprite Atlas (Sprite Atlas UI) Error 0 Answers