Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Raul 2 · Feb 17, 2011 at 10:56 PM · errormovementtypesourcedestination

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!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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];
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
â–¼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Raul 2 · Feb 18, 2011 at 11:06 AM 0
Share

Thanks i realized that like20 $$anonymous$$utes after posting this.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

Related Questions

Cannot cast from source to destination type 1 Answer

"Cannot cast from source type to destination type"- instantiating 4 Answers

InvalidCastException: Cannot cast from source type to destination type. 4 Answers

Expecting ':' Found '=' Error 1 Answer

Making side-scrolling movement? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges