Gui Text Script Not working

I am trying to make a general purpose gui text to display dialog, messages, and other ingame info. I made a gui.text and name it gui_message, and then I added a collison script to my player. Here is my code.

Message.js on gui_message

static var MESSAGE;

function Update ()
{
	guiText.text = "" + MESSAGE;

}

Collison.js on player

function OnCollisionEnter(collision : Collision) 
{

	var Message = GameObject.Find("gui_message");
	
//t_shop is the triggerbox 

	if (collision.gameObject.tag == "t_shop")
	{
		Message.MESSAGE = "This is the shop";	
	}

}

Unity return with the error on

Message.MESSAGE = "This is the shop";

saying it is not a unity game object. Anyone can help me out?

Idk about the second 2, but I know the first one needs to be like this…

static var MESSAGE;

function Update ()
{
guiText.text = “” + MESSAGE__.ToString()__;

}

I believe, it always works that way with me. :smile: hope it helped.

This isn’t the best way…the type of “message” should be defined as a string (the convention is lower-case for variable names), so that way you wouldn’t need to do string concatenation, and also it shouldn’t be in Update, since there is no reason to assign it every frame. Just make a function that you call when you want to actually change the message:

function DisplayMessage (message : String) {
	guiText.text = message;
}

–Eric

I have been trying your method but don’t seems to get the result, I think I am just weak in programming.

So my gui text now have

function DisplayMessage (message : String) {
	guiText.text = message;
}

Now how do I call this function from another script such as my collision.js and do I still need the static variable?

You don’t need the static variable. See here about accessing other game objects.

–Eric

Ok so I am getting stuck on this again.

so my Message.js with the function DisplayMessage is attach to my gui_message.

Now I have trouble calling this function, I try to use a few example from the link but still no dice.

function OnCollisionEnter(collision : Collision) 
{
	if (collision.gameObject.tag == "t_shop")
	{
		message = GetComponent(Message);
		message.DisplayMessage("This is a Shop"); 
	}
}

Now the game compile fine, but as soon as I hit the trigger box I bouncing backward like there is a force field and error start repeating:

NullReferenceException: Object reference not set to an instance of an object.

Help please?

   if (collision.gameObject.tag == "t_shop")
	{
		message = GetComponent(Message);
		message.BroadCastMessage('DisplayMessage","This is a Shop"); 
	}

Is the Message script a component of the game object that the OnCollisionEnter script is attached to?

Thanks for the note Eric… will keep it in mind