- Home /
Function call won't work...
Ok, so I have a script called MessageLog. It is basically a nice GUI box. You simply call the function AddMessage(message) and the message will be displayed in the box. It works just fine.
So, in a game object called Manager, I have the MessageLog attached, and another script called ManagerCode.
Inside ManagerCode, I add a message to my log like this:
gameObject.GetComponent("MessageLog").AddMessage("Welcome!!");
I call that once in the Awake() function and in the Update() function (around every 300 frames).
So, I can tell it works just fine.
Anyway, in the Manager script I have a function like this:
function Whatever (identification) {
gameObject.GetComponent("MessageLog").AddMessage("TEST");
}
identification
is just an array with some data for later use. The problem is when I try to send the "TEST" message to my log. My log simply won't display the message. And I am clueless.
Proven Facts:
Function Whatever(identification) is being called. gameObject Manager has the MessageLog component script. MessageLog script does receive the "TEST" message. The only message the MessageLog script won't display, is the one coming from the Whatever function. All other messages received from any other place is received and displayed properly. The OnGUI method of the messagelog is called.
The AddMessage function is
function AddMessage (message) {
messages[4] = messages[3];
messages[3] = messages[2];
messages[2] = messages[1];
messages[1] = messages[0];
messages[0] = message;
Debug.Log(message); // Console indicates we DID receive the "TEST" message...
}
Answer by Toxic Blob · Nov 24, 2010 at 01:18 AM
It'd probably be better to cache the component lookup. This is better for speed (so I am told), and also cleans up your code. Try this to see if this clears up your problem.
private var messageLog:MessageLog;
function Awake()
{
messageLog = gameObject.GetComponent("MessageLog") as MessageLog;
}
then, wherever you need to call it in your Manager script just type:
messageLog.AddMessage("TEST");
Hmm, thank you. It didn't fix the problem directly, but I think I am closer. For some reason, now I get this error message from the line I'm calling the Add$$anonymous$$essage function:
NullReferenceException: Object reference not set to an instance of an object
Any ideas?
Because the function has not been initialised by the time you're attempting to run it. Rather than having it as a component, you could simply call it. Try:
private var messageLog:$$anonymous$$essageLog = new $$anonymous$$essageLog();
then...
messageLog.Add$$anonymous$$essage("TEST");
"You are trying to create a $$anonymous$$onoBehaviour using the 'new' keyword. This is not allowed. $$anonymous$$onoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all" - Pointing at my $$anonymous$$essageLog script.
O$$anonymous$$ I have done even more testing. In the Whatever() function it seems messageLog is null. But I don't understand that, if I can call the Add$$anonymous$$essage function every frame in the Update() function!
So before calling Add$$anonymous$$essage on this null messageLog, I placed the line: messageLog = gameObject.GetComponent("$$anonymous$$essageLog") as $$anonymous$$essageLog; and now no error will appear. However, the messagelog still won't display a my test message!
Your answer
Follow this Question
Related Questions
OnTriggerEnter function in c# 0 Answers
Functions and Rigibodies 1 Answer
Weird Code Error Message 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers