- Home /
Error BCW0015 and BCW0023 but they both work, how to solve it?
Hi Guys,
I'm working on a Android application in Unity I have wrote a script, that works perfectly however the engine gives me to warning. Can you help me.
#pragma strict
var async: AsyncOperation = null;
var TText : TextMesh;
private var Tfloar : float;
private var Tint : int;
private var Once : boolean = true;
private var Text1 : String = "info1";
private var Text2 : String = "info2";
private var Text3 : String = "info3";
private var Text4 : String = "info4";
private var Text5 : String = "info5";
private var Text6 : String = "info6";
private var Text7 : String = "info7";
private var Text8 : String = "info8";
private var Text9 : String = "info9";
private var Text10 : String = "info10";
private var Text11 : String = "info11";
private var Text12 : String = "info12";
private var Text13 : String = "info13";
private var Text14 : String = "info14";
private var Text15 : String = "info15";
private var Text16 : String = "info16";
private var Text17 : String = "info17";
private var Text18 : String = "info18";
private var Text19 : String = "info19";
private var Text20 : String = "info20";
var TLevelCount : LevelCount = null;
function Update ()
{
if (TLevelCount == null) // search for an object call LevelCountOb
{
TLevelCount = GameObject.Find("LevelCountOb").GetComponent(LevelCount);
}
if (TLevelCount != null) // if he found the object
{
switch (TLevelCount.LevelToLoad)
{
case 2:
if (Once)
{
Once = false;
Load();
TText.text = GetRandom();
}
break;
case 3:
if (Once)
{
Once = false;
Load();
TText.text = GetRandom();
}
break;
case 4:
if (Once)
{
Once = false;
Load();
TText.text = GetRandom();
}
break;
case 5:
if (Once)
{
Once = false;
Load();
TText.text = GetRandom();
}
break;
case 6:
if (Once)
{
Once = false;
Load();
TText.text = GetRandom();
}
break;
case 7:
if (Once)
{
Once = false;
Load();
TText.text = GetRandom();
}
break;
case 8:
if (Once)
{
Once = false;
Load();
TText.text = GetRandom();
}
break;
}
}
}
function Load()// load another level
{
if (TLevelCount != null && async == null)
{
async = Application.LoadLevelAsync(TLevelCount.LevelToLoad);
yield async;
}
}
function GetRandom(): String // get the information
{
var RandoT : int = Random.Range(1, 21);
print("the random number is: " +RandoT);
switch (RandoT)
{
case 1:
return Text1;
break;
case 2:
return Text2;
break;
case 3:
return Text3;
break;
case 4:
return Text4;
break;
case 5:
return Text5;
break;
case 6:
return Text6;
break;
case 1:
return Text1;
break;
case 7:
return Text7;
break;
case 8:
return Text8;
break;
case 9:
return Text9;
break;
case 10:
return Text10;
break;
case 11:
return Text11;
break;
case 12:
return Text12;
break;
case 13:
return Text13;
break;
case 14:
return Text14;
break;
case 15:
return Text15;
break;
case 16:
return Text16;
break;
case 17:
return Text17;
break;
case 18:
return Text18;
break;
case 19:
return Text19;
break;
case 20:
return Text20;
break;
}
}
Now this error Assets/Script/PreLoaderBar.js(137,10): BCW0023: WARNING: This method could return default value implicitly. happen on get random
and the second one Assets/Script/PreLoaderBar.js(143,9): BCW0015: WARNING: Unreachable code detected. the Switch.
The code works but I would like to know where is the problem.
Thank you
BCW0015 is probably related to your break after each return. If the function returns it never gets to the break. If you can highlight what your line 137 is, we can probably work out what that BCW0023 is.
Actually, spoke too soon. BCW0023 means that your function GetRandom
can get to the end of the switch statement, and hit the end of the function and not return anything. Or, put another way, if I call GetRandom(-42)
what string should I get?
I don't know unity script so this is just a guess, but I think you might simplify your code using:
#pragma strict
var async: AsyncOperation = null;
var TText : Text$$anonymous$$esh;
private var Tfloar : float;
private var Tint : int;
private var Once : boolean = true;
private var myText : String[] = [];
var TLevelCount : LevelCount = null;
function Start ()
{
myText = [
"info1", "info2", "info3", "info4", "info5", "info6", "info7", "info8", "info9",
"info10", "info11", "info12", "info13", "info14", "info15", "info16", "info17", "info18", "info19",
"info20"
];
}
function Update ()
{
if (TLevelCount == null) {
TLevelCount = GameObject.Find("LevelCountOb").GetComponent(LevelCount);
} else if ( Once ) {
Once = false;
Load(TLevelCount.LevelToLoad);
TText.text = GetRandom();
}
}
function Load(LevelToLoad : int)// load another level
{
if (async != null) { return; }
async = Application.LoadLevelAsync(LevelToLoad);
yield async;
}
function GetRandom(): String // get the information
{
var RandoT : int = Random.Range(0, myText.length);
print("the random number is: " + RandoT);
return myText[RandoT];
}
Answer by Diego89 · Apr 17, 2014 at 06:52 PM
thank you the problem was releated to the multiple return break