- Home /
InvalidCastException: Cannot cast from source type to destination type.
So i get this problem, Heres my code
var speed : float = 50.0; var move : int = 0; var currentPlayer : int = 1; var players: int = 2; var playerOne : Transform; var playerTwo : Transform; var playerOnePoints : int = 0; var playerTwoPoints : int = 0; var finalScore : int = 0; var questionAmount : float = 1.0; var questions : Array; var currentQuestion : int = 0; var questionCheck : int = 0; var n : int = 0; var questionAllUsed : boolean = false; var winnerBanner : boolean = false; var lastQuestionDone : boolean = false; var keepGoing : boolean = true; var numOfQuestions : int = 0; var correctA : boolean = false; var delay : float = 3.0; var done : float = 0.0; var waypoints1 : Array; var waypoints2 : Array; var customSkin : GUISkin; var questionBoard : Texture2D;
var spawn1 : Transform; var waypointOne1 : Transform; var waypointOne2 : Transform; var waypointOne3 : Transform; var waypointOne4 : Transform; var waypointOne5 : Transform; var waypointOne6 : Transform; var waypointOne7 : Transform; var waypointOne8 : Transform; var waypointOne9 : Transform; var waypointOne10 : Transform; var waypointOne11 : Transform; var waypointOne12 : Transform; var waypointOne13 : Transform; var waypointOne14 : Transform; var waypointOne15 : Transform; var seperation : boolean = false; var spawn2 : Transform; var waypointTwo1 : Transform; var waypointTwo2 : Transform; var waypointTwo3 : Transform; var waypointTwo4 : Transform; var waypointTwo5 : Transform; var waypointTwo6 : Transform; var waypointTwo7 : Transform; var waypointTwo8 : Transform; var waypointTwo9 : Transform; var waypointTwo10 : Transform; var waypointTwo11 : Transform; var waypointTwo12 : Transform; var waypointTwo13 : Transform; var waypointTwo14 : Transform; var waypointTwo15 : Transform;
function Start () {
currentQuestion = parseInt(Random.Range(0,3)); questions = [ ["question1","answer11","answer12","answer13","answer14","1","0"], ["question2","answer21","answer22","answer23","answer24","2","0"], ["question3","answer31","answer32","answer33","answer34","3","0"], ["question4","answer41","answer42","answer43","answer44","3","0"],
["question5","answer51","answer52","answer53","answer54","3","0"] ]; numOfQuestions = questions.length; Debug.Log(numOfQuestions); //waypoints for player 1 waypoints1 = [ [spawn1.position.x,spawn1.position.y,"0"], [waypointOne1.position.x,waypointOne1.position.y,"1"], [waypointOne2.position.x,waypointOne2.position.y,"2"], [waypointOne3.position.x,waypointOne3.position.y,"3"], [waypointOne4.position.x,waypointOne4.position.y,"4"], [waypointOne5.position.x,waypointOne5.position.y,"5"], [waypointOne6.position.x,waypointOne6.position.y,"6"], [waypointOne7.position.x,waypointOne7.position.y,"7"], [waypointOne8.position.x,waypointOne8.position.y,"8"], [waypointOne9.position.x,waypointOne9.position.y,"9"], [waypointOne10.position.x,waypointOne10.position.y,"10"], [waypointOne11.position.x,waypointOne11.position.y,"11"], [waypointOne12.position.x,waypointOne12.position.y,"12"], [waypointOne13.position.x,waypointOne13.position.y,"13"],
[waypointOne14.position.x,waypointOne14.position.y,"14"], [waypointOne15.position.x,waypointOne15.position.y,"15"]
]; //waypoints for player 2 waypoints2 = [ [spawn2.position.x,spawn2.position.y,"0"], [waypointTwo1.position.x,waypointTwo1.position.y,"1"], [waypointTwo2.position.x,waypointTwo2.position.y,"2"], [waypointTwo3.position.x,waypointTwo3.position.y,"3"], [waypointTwo4.position.x,waypointTwo4.position.y,"4"], [waypointTwo5.position.x,waypointTwo5.position.y,"5"], [waypointTwo6.position.x,waypointTwo6.position.y,"6"], [waypointTwo7.position.x,waypointTwo7.position.y,"7"], [waypointTwo8.position.x,waypointTwo8.position.y,"8"], [waypointTwo9.position.x,waypointTwo9.position.y,"9"], [waypointTwo10.position.x,waypointTwo10.position.y,"10"], [waypointTwo11.position.x,waypointTwo11.position.y,"11"], [waypointTwo12.position.x,waypointTwo12.position.y,"12"], [waypointTwo13.position.x,waypointTwo13.position.y,"13"],
[waypointTwo14.position.x,waypointTwo14.position.y,"14"], [waypointTwo15.position.x,waypointTwo15.position.y,"15"]
];
playerOne.transform.position.x = spawn1.position.x;
playerOne.transform.position.y = spawn1.position.y;
playerTwo.transform.position.x = spawn2.position.x;
playerTwo.transform.position.y = spawn2.position.y;
}
// buttons and questions function OnGUI () { GUI.skin = customSkin; // Make a background box
GUI.Label (Rect (25,500,4000,20),"Player Turn: " + currentPlayer); GUI.Label (Rect (25,50,4000,20),"Player 1 Score " + playerOnePoints); GUI.Label (Rect (25,100,4000,20),"Player 2 Score " + playerTwoPoints);
GUI.Label (Rect (Screen.width - (Screen.width/2)-200,Screen.height - (Screen.height/2)-350,700,550),questionBoard);
GUI.Label (Rect (Screen.width - (Screen.width/2)+10,Screen.height - (Screen.height/2)-250,700,550),questions[currentQuestion][0]);
GUI.Box (Rect (Screen.width - (Screen.width/2)-60,Screen.height - (Screen.height/2)-55,300,40), questions[currentQuestion][1]);
GUI.Box (Rect (Screen.width - (Screen.width/2)-60,Screen.height - (Screen.height/2)+10,300,40), questions[currentQuestion][2]);
GUI.Box (Rect (Screen.width - (Screen.width/2)-60,Screen.height - (Screen.height/2)+80,300,40), questions[currentQuestion][3]);
//GUI.Label (Rect (85,240,4000,20), questions[currentQuestion][4]);
if( winnerBanner && lastQuestionDone )
{
GUI.Label (Rect (155,150,4000,20),"End of game " + currentPlayer + " Congratulations with ");
}
// correct choice
if(correctA)
{
GUI.Label (Rect (500,500,4000,20),"CORRECT!");
}
// Make the first button. If it is pressed, Application.Loadlevel (1) will be executed
if (GUI.Button (Rect (Screen.width - (Screen.width/2)-72,Screen.height - (Screen.height/2)-60,60,40), "[ ]")) {
questions[currentQuestion][6] = "1";
if(questionAllUsed)
{
lastQuestionDone = true;
}
if(questions[currentQuestion][5] == "1"){
move = 1;
Debug.Log("A1");
if(currentPlayer == 1)
{
playerOnePoints++;
playerOne.transform.position.x = waypoints1[playerOnePoints][1];
playerOne.transform.position.y = waypoints1[playerOnePoints][2];
}
if(currentPlayer == 2)
{
playerTwoPoints++;
playerTwo.transform.position.x = waypoints2[playerTwoPoints][1];
playerTwo.transform.position.y = waypoints2[playerTwoPoints][2];
}
}
else
{move = 2;}
}
// Make the second button.
if (GUI.Button (Rect (Screen.width - (Screen.width/2)-72,Screen.height - (Screen.height/2),60,40), "[ ]")) {
questions[currentQuestion][6] = "1";
if(questionAllUsed)
{
lastQuestionDone = true;
}
if(questions[currentQuestion][5] == "2"){
move = 1;
Debug.Log("A2");
if(currentPlayer == 1)
{
playerOnePoints++;
playerOne.transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
if(currentPlayer == 2)
{
playerTwoPoints++;
playerTwo.transform.Translate(Vector3.forward * Time.deltaTime * speed);
}
}
else{move = 2;}
}
// Make the second button.
if (GUI.Button (Rect (Screen.width - (Screen.width/2)-72,Screen.height - (Screen.height/2)+72,60,40),"[ ]")) {
questions[currentQuestion][6] = "1";
if(questionAllUsed)
{
lastQuestionDone = true;
}
if(questions[currentQuestion][5] == "3"){
move = 1;
Debug.Log("A3");
if(currentPlayer == 1)
{
correctA = true;
playerOnePoints++;
playerOne.transform.Translate(Vector3.forward * Time.deltaTime * speed);
correctA = false;
}
if(currentPlayer == 2)
{
correct = true;
playerTwoPoints++;
playerTwo.transform.Translate(Vector3.forward * Time.deltaTime * speed);
correct = false;
}
}
else{move = 2;}
}
if( lastQuestionDone )
{
if(playerOnePoints > playerTwoPoints)
{
winnerBanner = true;
finalScore = playerTwoPoints;
}
if(playerTwoPoints > playerOnePoints)
{
winnerBanner = true;
finalScore = playerTwoPoints;
}
if(playerOnePoints == playerTwoPoints)
{
winnerBanner = true;
finalScore = playerTwoPoints;
}
}
else
{
GUI.Label (Rect (25,500,4000,20),"Player Turn: " + currentPlayer);
GUI.Label (Rect (25,100,4000,20),"Player 2 Score " + playerTwoPoints);
GUI.Label (Rect (25,50,4000,20),"Player 1 Score " + playerOnePoints);
questionCheck = 0;
while(questions[currentQuestion][6] == "1")
{
currentQuestion = parseInt(Random.Range(0,numOfQuestions-1));
if(questionCheck > numOfQuestions*2)
{
keepGoing = true;
for(n = 0; n < numOfQuestions && keepGoing; n++)
{ Debug.Log(n + "NNNNNNNN");
if( questions[n][6] == "0")
{
currentQuestion = n;
n = numOfQuestions;
keepGoing=false;
}
}
if(n > numOfQuestions)
{
questionAllUsed = true;
}
}
questionCheck++;
}
}
}
function Update() { // currentPlayer 1 if (lastQuestionDone == false) {
if( currentPlayer == 1 ) { if(move != 0) { currentPlayer = 2; move = 0;
} } //currentPlayer 2 if( currentPlayer == 2 ) { if(move != 0) { currentPlayer = 1; move = 0;
}
}
}
else
{
}
}
Whole error:
InvalidCastException: Cannot cast from source type to destination type.
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible)
Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value)
Boo.Lang.Runtime.RuntimeServices.UnboxSingle (System.Object value)
Movementtest1.OnGUI () (at Assets/My assest/scripts/Movementtest1.js:161)
So tell me whats wrong, everythime the player gets a question right, They move up one space. YES I KNOW MY CODE IS NOT GOOD! I am just trying to get this done for a presentation, hard-coding if i have too!
Answer by DaveA · Feb 18, 2011 at 05:11 AM
I think you want to use 0,1 rather than 1,2, because the contents of those waypoints is float,float,string, right?
playerOne.transform.position.x = waypoints1[playerOnePoints][0];
playerOne.transform.position.y = waypoints1[playerOnePoints][1];
Thanks i realized that like20 $$anonymous$$utes after posting this.