- Home /
 
Changing a GUIStyle for a button from Java Script to C#
Hey, I've been using Java Script for my game scripts but need to switch to C#. I have a GUI button that I am trying to customize. Here is the code in Java Script:
 function OnGUI()
 {
     var buttonStyle= new GUIStyle("Button");
     buttonStyle.fontSize = 20;
     buttonStyle.normal.textColor = Color.black;
     buttonStyle.alignment = TextAnchor.UpperCenter;
 
 GUI.Button(new Rect(0,0,100,100),"Button",buttonStyle));
 }
 
               How do I accomplish this in C#?
Thanks!
Here's some links I found useful in converting between C# and JS :
Those links are very helpful thank you! The one thing I cannot find however is how to specify that the GUIStyle is a button. If I just create a public GUIStyle and change the color and size settings in Unity, the GUI component appears as a label ins$$anonymous$$d of a button.
Answer by s_ruffin · Jul 19, 2013 at 01:40 PM
 private void OnGUI()
 {
 GUIStyle buttonStyle = new GUIStyle(GUI.skin.button);  //Keeps the button looking like a button
 buttonStyle.fontSize = 30;  //changes font size of button
 
 GUI.button(new Rect(0,0,100,100),"",buttonStyle);  //creates button
 }
 
               I figured it out, thanks!
Answer by Jamora · Jul 19, 2013 at 12:27 PM
Change function to void. Then put the function in the class which is automatically generated by Unity as you create the C# file.
Your answer
 
             Follow this Question
Related Questions
GUI Style Issue-- Texture not changing 2 Answers
NullreferenceException, BeginScrollView style change 3 Answers
Distribute terrain in zones 3 Answers
Can you use GUIStyle with GUILayout? 0 Answers