- Home /
Unable to cast from the source?
I am making a simple script that changed GUI Text for people and manages what they say. But when I run this now unity says it is unable to cast from source type to destination type.
var inputkey1 = "1"; var inputkey2 = "2"; var inputkey3 = "3"; var inputkey4 = "4"; var inputmessage1 = "Dont Put the same inputer key in 2"; var inputmessage2 = "or it will print both lines at once!"; var inputmessage3 = "This is what inputkey3 will say!"; var inputmessage4 = "This is what inputerkey4 will say!"; private var gt : GUIText; function Start() { gt.text = GetComponent(GUIText); if( gt == null ) Debug.Log("No GUIText component found"); }
function Update () { if(Input.GetKeyDown(inputkey1)) { print ( inputmessage1 ); gt.text = inputmessage1; }
else if(Input.GetKeyDown(inputkey2)) { print ( inputmessage2 ); gt.text = inputmessage2; }
else if(Input.GetKeyDown(inputkey3)) { print ( inputmessage3 ); gt.text = inputmessage3; }
else if(Input.GetKeyDown(inputkey4)) { print ( inputmessage4 ); gt.text = inputmessage4; } }
Answer by rudolfwm · Feb 19, 2011 at 09:33 AM
Problem is here:
gt.text = GetComponent(GUIText)
this should be
gt = GetComponent(GUIText)
(not text, which expects a string).
You might as well use the built-in reference: gt = guiText;
Your answer
Follow this Question
Related Questions
Cannot cast from source to destination type 1 Answer
InvalidCastException: Cannot cast from source type to destination type. 3 Answers
"Cannot cast from source type to destination type" 2 Answers
"Cannot cast from source type to destination type"- instantiating 4 Answers
InvalidCastException: Cannot cast from source type to destination type. 4 Answers