- Home /
Trying to make a dynamic error logging class, running into some trouble
I want to pass the gameobject, and the current component into the error logger and have it print out some debug.log.
Here's what I currently have for the error logger:
public class ErrorLog : MonoBehaviour {
public ErrorLog(){
}
public ErrorLog(GameObject relevantObject, objectrelevantComponent, string methodName, string additionalInfo){
Debug.Log ("<----------- ERROR MESSAGE ----------->");
string gOName = relevantObject.name.ToString();
string cOName = relevantComponent.GetType().Name;
string timeStamp = Time.time.ToString();
Debug.Log ("At " + timeStamp);
Debug.Log ("GameObject " + gOName + " has encountered an error");
Debug.Log ("inside the " + cOName + " component while using the " + methodName + " function");
Debug.Log (additionalInfo);
Debug.Log ("<----------- END MESSAGE ----------->");
}
}
and heres how I'm trying to call it:
ErrorLog(gameObject, WhateverClass, "GetKeyCode", "NONE");
The problem is that it returns this in the editor:
Expression denotes a `type', where a `variable', `value' or `method group' was expected
I've tried using typeOf on the class name. but it didnt seem to change anything. Does anybody know how I can make this work?
I was able to get it working but not how I want it to work. I cant call the class itself I had to change the error log so that the functionality is in a function called error message. I think instantiate it into the class I want to call it from and call errorLog.Error$$anonymous$$essage(); This is less than ideal and I need to learn how to make it work more properly. I'm not totally sure of all the ways I can use a class.
What about having a regular class (no $$anonymous$$onobehaviour) with a static method in it? You then can call it from anywhere anytime to write formatted output to the console. Isn't that exactly what you want?
Answer by Jake-L · Jul 01, 2015 at 07:26 PM
Your parameter "relevantComponent" is a string and you try to pass a class. Either change the parameter to "object relevantComponent" or call it with WhateverClass.GetType().Name.
It appears the problem is happening with gameobject.
Your answer
![](https://koobas.hobune.stream/wayback/20220613201619im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
declaring unknown script components under a empty variable 0 Answers
Create a method that gets called everytime something happens, passing a Transform component to it 1 Answer
I can't use Rigidbody2D in my scripts, I can't find rigidbody2d in my script 3 Answers