x


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

asked Feb 19 '11 at 08:30 AM

DevonJS gravatar image

DevonJS
28 10 12 16

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

1 answer: sort newest

Problem is here:

gt.text = GetComponent(GUIText)

this should be

gt = GetComponent(GUIText)

(not text, which expects a string).

more ▼

answered Feb 19 '11 at 09:33 AM

rudolfwm gravatar image

rudolfwm
119 2 2 11

You might as well use the built-in reference: gt = guiText;

Feb 19 '11 at 12:14 PM Peter G
(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:

x3460
x69
x54
x18
x4

asked: Feb 19 '11 at 08:30 AM

Seen: 540 times

Last Updated: Feb 19 '11 at 08:30 AM