- Home /
InvalidCastException
This is literally all the code I have:
var Target = Transform;
var distanceFromPlayer= 10;
function LateUpdate() {
transform.position = Target.position.GameObject + Vector3(distanceFromPlayer,0,0);
}
but I can't get it function properly. I constantly get a 'InvalidCastException' error
Full error: InvalidCastException: Cannot cast from source type to destination type. (wrapper dynamic-method) UnityEngine.Transform.Transform$get_position$ (object,object[]) Boo.Lang.Runtime.RuntimeServices.GetProperty (object,string) UnityScript.Lang.UnityRuntimeServices.GetProperty (object,string) Platformer Camera.LateUpdate () (at Assets/Platformer Camera.js:5)
Help please. :(
Answer by save · Aug 15, 2013 at 01:44 PM
Change var Target = Transform;
to var Target : Transform;
. You have been trying to assign Transform as the class, I figure you want a type reference to transform. This will make it pop-up in the inspector. To assign the transform of the current object do this:
var Target : Transform;
function Start () {
Target = transform; // transform is the component, Transform is the class
}
Remove GameObject
in transform.position = Target.position.GameObject
.
still get this error: InvalidCastException: Cannot cast from source type to destination type. (wrapper dynamic-method) UnityEngine.Transform.Transform$get_position$ (object,object[]) Boo.Lang.Runtime.RuntimeServices.GetProperty (object,string) UnityScript.Lang.UnityRuntimeServices.GetProperty (object,string) Platformer Camera.LateUpdate () (at Assets/Platformer Camera.js:5)
I wasn't even meant to have that in there, that was just cuz I got frustrated and started messing about and reading other topics. Not sure what is wrong.
The video that I am using for reference is : http://www.youtube.com/watch?v=1UQqb$$anonymous$$B$$anonymous$$q-$$anonymous$$ and he gets a line where he is able to choose a target, and drop his 'player' into, but $$anonymous$$e is just not there, and I am guessing it is due to this error.
Answer is updated. You were trying to assign the class ins$$anonymous$$d of type referencing.
Your answer
![](https://koobas.hobune.stream/wayback/20220613112255im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
How to approach different game modes 2 Answers
Shaders question, Shield hit effect 1 Answer
how to pass a function to a function 1 Answer
Unable to find problems with 2d-scrolling movement code. 2 Answers