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 ()

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

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