x


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);
}
}
more ▼

asked Apr 27 '11 at 05:21 AM

MK7 gravatar image

MK7
66 25 26 30

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Apr 27 '11 at 05:36 AM

by0log1c gravatar image

by0log1c
2.1k 6 9 18

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#).

Apr 27 '11 at 05:44 AM Eric5h5

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

Apr 27 '11 at 05:47 AM MK7

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

@MK7 Give it a try and ask a separate question if needs be!

Apr 27 '11 at 05:58 AM by0log1c
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1949
x1528
x69
x54
x18

asked: Apr 27 '11 at 05:21 AM

Seen: 1669 times

Last Updated: Apr 27 '11 at 05:21 AM