- Home /
InvalidCastException
For some reason, this error keeps popping up
(wrapper dynamic-method) UnityEngine.GameObject.GameObject$GetComponent$ (object,object[]) Boo.Lang.Runtime.RuntimeServices.Invoke (object,string,object[]) UnityScript.Lang.UnityRuntimeServices.Invoke (object,string,object[],System.Type) ButtonClick..ctor () (at Assets/scripts/ButtonClick.js:1) and i think i've got everything right, because it's working the way i want it to i've got this script on one object var Puzzpillar01 : PillarSpin = GameObject.GetComponent(PillarSpin.js);InvalidCastException: Cannot cast from source type to destination type.
function OnMouseDown(){ animation.Play("ButtonPress"); Puzzpillar01.spin(); } and this one on another var position : String = "0";
function spin () { if (position == "0") { position = "1"; animation.Play("Puzzpillarspin0"); } else if (position == "1") { position = "2"; animation.Play("Puzzpillarspin1"); } else if (position == "2") { position = "3"; animation.Play("Puzzpillarspin2"); } else if (position == "3") { position = "4"; animation.Play("Puzzpillarspin3"); } else if (position == "4") { position = "5"; animation.Play("Puzzpillarspin4"); } else if (position == "5") { position = "0"; animation.Play("Puzzpillarspin5"); } } and everything seems fine, but the error still keeps popping up. is there a problem with my code?
Answer by Professor Snake · Nov 09, 2013 at 09:14 PM
You most likely need to replace GetComponent(PillarSpin.js) with GetComponent(PillarSpin). File extentions aren't included in the class names.
In which case. Try replacing GameObject.GetComponent with just GetComponent.
it gets rid of the invalidcastexception error, but now i get unityexception and argumentexception errors ins$$anonymous$$d
I don't think you are allowed to use GetComponent in a declaration outside of a function. Citation needed.
Try it in Start()
Your answer
Follow this Question
Related Questions
Error on HSController.js 1 Answer
iTween: Cannot cast from source type to destination type. 1 Answer
Sprite Root Cannot cast from source type to destination type. 0 Answers
Sytem.InvalidCastException on Windows Phone 1 Answer
Door Script Help! 0 Answers