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 MK7 · Apr 27, 2011 at 05:21 AM · errorraycastcastsourcedestination

Cannot cast from source to destination type

I keep getting this error and I haven't a clue as to why; other than it has to do with this script. Please help me understand why this is happening

var Player : Transform; var rotateAmount = 90; var controller : CharacterController; var Hit : RaycastHit;

function Start(){ } function FixedUpdate (){
//casts a ray from camera to point in world var ray = Camera.main.ScreenPointToRay (Input.mousePosition); // If the controller is grounded and the ray hits an object; then you can Rotate walls if ((controller.isGrounded) && (Physics.Raycast (ray, Hit, 50))) { canRotate =true;
var Hitobject : GameObject = Hit.transform.gameObject && Hit.collider; var pivot : Transform = Hitobject.transform; if (Hitobject.tag == "Left"){ if(Input.GetMouseButtonDown(0)) RotateObjectForward(Player.position,pivot.forward, 90,1); if(Input.GetMouseButtonDown(1)) RotateObjectForward(Player.position,-pivot.forward,90,1); } }

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
1
Best Answer

Answer by by0log1c · Apr 27, 2011 at 05:36 AM

I'm not certain that is all there is to it but :

var Hitobject : GameObject = Hit.transform.gameObject && Hit.collider;

should probably be:

var Hitobject : GameObject = Hit.transform.gameObject;

A "Cannot cast from source to destination type" is well... an invalid-cast error. In other words, you're trying to reference an incorrect type into a type-restricted variable. For examples:

var number:int = "ThisIsNotAnInt";
var gameObject:GameObject = 9000;
var custom:MyCustomType = "NotMyCustomType";

would - I believe - all throw the exception. Note that JScript allow for dynamic casting, such as:

var unrestrictedType = "ICanJustReferenceWhateverTypeIWantHere";
var unrestrictedType = 1337;
var unrestrictedType = 3.1415926535;

would - I believe - never throw the exception, though errors might arise later.

Comment
Add comment · Show 3 · 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 Eric5h5 · Apr 27, 2011 at 05:44 AM 0
Share

Just to note, that's not dynamic casting, that's type inference. It's not limited to JS; C# also does that (the last few lines would compile as-is in C#). It infers the type from the value supplied when the variable is declared, but you can't change the type later, so it's not dynamic. Doing "`var blah = "foo";`" (JS/C#) is absolutely identical to doing "`var blah : String = "foo";`" (JS) or "`string blah = "foo";`" (C#).

avatar image MK7 · Apr 27, 2011 at 05:47 AM 0
Share

Ah thank you! you were absolutely right about that part. Now I just need to figure out why my tag won't work.

avatar image by0log1c · Apr 27, 2011 at 05:58 AM 0
Share

Ahh, I didn't know C# allowed type inference. Then I can keep my overloading functions untyped when converting to C#... Awesome news :D!

@$$anonymous$$$$anonymous$$7 Give it a try and ask a separate question if needs be!

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

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

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

InvalidCastException: Cannot cast from source type to destination type. 1 Answer

unable to cast from source to destination 0 Answers

Cannot cast from source type to destination type ERROR! 2 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