- Home /
Need help converting js to C# -- GUIStyle
Hi! Need help to convert to C#
JS code:
GUIStyle style;
void OnGUI ()
{
style.fontSize=24;
style.normal.textColor=Color.red;
style.alignment=TextAnchor.MiddleCenter;
GUI.Label ( new Rect(Screen.width/2-300, Screen.height/2-20, 600, 40), "TEST",style);
}
it was Js code... i converted it but have error in this line:
style.fontSize=24; //NullReferenceException: Object reference not set to an instance of an object winLostNew.OnGUI ()
Answer by Seth-Bergman · Oct 14, 2012 at 09:08 AM
you mean you want to convert this C# to javascript.. ok:
var style : GUIStyle;
function OnGUI ()
{
style.fontSize=24;
style.normal.textColor=Color.red;
style.alignment=TextAnchor.MiddleCenter;
GUI.Label ( Rect(Screen.width/2-300, Screen.height/2-20, 600, 40), "TEST",style)
}
(javascript)
your code is already in c#.. of course you would need to encapsulate this in a class (which inherits from monobehavior) with C#, as this is not handled implicitly as with javascript-based unityscript.. but this is created automatically when you create a new c# script from within unity..
EDIT :
my mistake, I should have paid closer attention to the question. the data member "style" is probably private by default. If you declare it public:
public GUIStyle style;
then you could access it directly via the inspector to set this all up at any rate, and I suspect this may somehow be causing the issue at hand, though I have no idea why..
http://docs.unity3d.com/Documentation/Components/class-GUIStyle.html
No to C#... it was Js code... i converted it but have error in this line:
style.fontSize=24; //NullReferenceException: Object reference not set to an instance of an object winLostNew.OnGUI ()
ah.. you simply need to set style to the appropriate object, as the error says.. you could simply drag and drop in the inspector
sorry, no, on second look you probably just need to do something like this:
GUIStyle style = new GUIStyle;
So how to do it?? So i just change "GUIStyle style;" to "public GUIStyle style;" and that all?
hmm, on further investigation I don't think you should have to do that though.. though I would assume the default scope is "public" anyway, you might add that:
public GUIStyle style;
otherwise this should work though..
right sorry, just noticed your comment.. like this:
public GUIStyle style = new GUIStyle();
as @echofiend correctly points out in his answer, it was the initialization that was the issue.
Answer by echofiend · Oct 14, 2012 at 10:51 AM
You have to first declare your style as a new style.
GUIStyle style = new GUIStyle();//Declare it a new object
void OnGUI ()
{
style.fontSize=24;
style.normal.textColor=Color.red;
style.alignment=TextAnchor.MiddleCenter;
GUI.Label ( new Rect(Screen.width/2-300, Screen.height/2-20, 600, 40), "TEST",style);
}
in C# you have to declare a new object before you can use it
Your answer
Follow this Question
Related Questions
js to C# - Instantiate(scoreParticle, object - Vector3(0,0,0.3), Quaternion.identity) 1 Answer
Need help C# -- yield, destroy 1 Answer
Need help converting js to C 1 Answer
Need help converting js to C# - rigidbody.rotation.z 1 Answer
Enemy AI script causing Unity to crash (Javascript to C#) 1 Answer