- Home /
switch always defaults
Hi there, I have a gameObject with the following function:
function SetDigit(digit)
{
switch(digit)
{
case("0"):
print("zero");
break;
case("1"):
print("one");
break;
case("2"):
print("two");
break;
default:
print("this should not happen");
print(digit);
break;
}
}
And I use another game object to call that function, giving it a string:
function UpdateNumber(number)
{
numberString=number.ToString();
var backwardsCounter=numberString.Length-1;
for(var digit in digits)
{
if(backwardsCounter>=0)
{
var myDigit=numberString[backwardsCounter];
digit.SendMessage("SetDigit",myDigit);
backwardsCounter--;
}
}
}
however, in the console I always see "this should not happen" and "1", even though 1 is handled in my switch :/
Ok, i'm a bit closer. I did a typeof() my variable and it's a System.Char, but how can I set the switch up for a char or convert the char to an integer?
Answer by Somian · Nov 30, 2011 at 09:08 PM
Ok, I figured.
Kludge, though x.X
function SetDigit(digit)
{
//we determine the target rotation using a lookup table
var malz:String="digit_";
malz=malz+digit;
switch(malz)
{
case("digit_0"):
print("zero");
break;
case("digit_1"):
print("one");
break;
case("digit_2"):
print("two");
break;
default:
print("this should not happen");
print(malz);
print(typeof(malz));
break;
}
}
when you mean an integer you do not use quotation marks This is a string: "1" This is the number one: 1
Your answer
Follow this Question
Related Questions
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Setting Scroll View Width GUILayout 1 Answer
Switch Statements 1 Answer
Switch Statement question 0 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers