- Home /
NullReferenceException when using GUIStyle to change text color
I'm using OnGUI() to display text on the screen.
The Text is colored white, so I used GUIStyle to change the color to black as so
GUIstyle.font.material.color = Color.black;
I get this error:
Test.Start () (at Assets/Scripts/s_Test.cs:15) Can anyone help me with this? Thanks in advanceNullReferenceException: Object reference not set to an instance of an object
Answer by dannyskim · Jan 29, 2012 at 10:53 PM
OnGUI elements don't utilize a traditional mesh based GUI system to display themselves. Therefor, there are no materials associated with them either.
If you were to utilize the older GUI Elements such as GUITexture, or GUIText, these accessors would still apply.
In order to access these variables, you can either access them through the Inspector if you expose your GUIStyle as a public variable instead of instantiating it at runtime, or you can access it through:
GUIstyle.normal.textColor
When you browse through the GUIStyle in the inspector, you'll see that there are several different states that you can edit, such as normal, Hover, Active, Focused, etc. You can then change these colors accordingly by accessing them through their appropriate methods.
Thanks, it worked perfectly. I don't have that error anymore.