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?
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;
}
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.