- Home /
NullreferenceException, BeginScrollView style change
Hi there Unity fokes, I am in need of a bit of help here. In a game project, we are trying to create a chat system, and for that chat system I have decided that it will be useful to use the Unity GUI. In that case, we have programmed a chat window using GUI.BeginScrollView, so the player can scroll up and down in the chatbox. When using BeginScrollView, you get two 'not so nice' scrollbars and my intention is to hide them, but no matter what I do, I can't seem to remove them. I attempt to execute the BeginScrollView this way:
GUI.BeginScrollView(
new Rect(0, 0, 220, 300),
scrollPosition,
new Rect(0, 0, 220, 350),
false,
false,
chatSkin.horizontalScrollbar,
chatSkin.verticalScrollbar
);
Where chatSkin is a GUISkin. But by doing this, I get the following error:
NullReferenceException: Object reference not set to an instance of an object
UnityEngine.GUI.BeginScrollView (Rect position, Vector2 scrollPosition, Rect viewRect, Boolean alwaysShowHorizontal, Boolean alwaysShowVertical, UnityEngine.GUIStyle horizontalScrollbar, UnityEngine.GUIStyle verticalScrollbar, UnityEngine.GUIStyle background).
The following error disappears if I remove the two last arguments in the method call "chatSkin.horizontalScrollbar" and "chatSkin.verticalScrollbar". The GUISkin chatSkin is defined in top of the script (haven't copied that though). The mySkin GUISkin has been assigned in the inspector.
Taking into considerations its a NullreferenceException, I have been glancing in the Inspector of chatSkin, though no matter what I do, I can't seem to get rid of this error. I hope the community out there can help me. Here is the rest of the function, best regards - Marc Pilgaard
private void ChatWindow()
{
Vector2 scrollPosition = Vector2.zero;
GUI.BeginGroup(new Rect(Screen.width/1.25f, Screen.height/1.25f - 300, 800, 600));
string focusWindow = GUI.GetNameOfFocusedControl();
GUI.BeginScrollView(
new Rect(0, 0, 220, 300),
scrollPosition,
new Rect(0, 0, 220, 350),
false,
false,
chatSkin.horizontalScrollbar,
chatSkin.verticalScrollbar
);
GUI.Box(new Rect(0, 0, 220, 300), CurrentChat(""), chatSkin.box);
GUI.EndScrollView();
GUI.SetNextControlName("chatfield");
stringToEdit = GUI.TextArea(new Rect(0, 302, 220, 50), stringToEdit, 200, chatSkin.textArea);
GUI.EndGroup();
if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Return && focusWindow == "chatfield")
{
GUIUtility.keyboardControl = 0;
CurrentChat(stringToEdit);
stringToEdit = "";
}
else if (Event.current.type == EventType.KeyUp && Event.current.keyCode == KeyCode.Return && !(focusWindow == "chatfield"))
{
GUI.FocusControl("chatfield");
}
}
Here are my chatSkin settings from the inspector: 125125
125125
Answer by Seth-Bergman · Aug 06, 2012 at 06:58 PM
http://docs.unity3d.com/Documentation/ScriptReference/GUI.BeginScrollView.html
try this:
GUI.skin = chatSkin;
GUI.BeginScrollView(
new Rect(0, 0, 220, 300),
scrollPosition,
new Rect(0, 0, 220, 350),
false,
false
);
it will accomplish the same thing
I believe the problem stems from trying to use a GUISkin, where a GUIStyle is required, not very familiar with GUIStyles though..
I don't know why, but this actually sorted it out. Though I have no clue why it did work. Thank you man, I appreciate it. (Thank you also Sundar, though I am going to give the Answer to Seth Bergman)
basically, if you leave out those optional parameters, the current GUISkin which is implemented is used by default..
GUI.skin = someSkin;
sets that skin to the GUI, so any GUI elements created will also use the same skin until you change it again:
GUI.skin = skinA;
if(GUI.Button.....// uses skinA
GUI.Box(Rect.....//uses skinA
GUI.skin = skinB;
GUI.Box(Rect.....//uses skinB
etc..
Answer by Bunny83 · Aug 06, 2012 at 07:03 PM
Keep in mind that scrollbars needs several styles. That's why it's usually better to just switch the whole skin. Unity determines the other style names like this:
GUI.skin.GetStyle(style.name + "thumb")
GUI.skin.GetStyle(style.name + "upbutton")
GUI.skin.GetStyle(style.name + "downbutton")
// or those for the horizontal version:
GUI.skin.GetStyle(style.name + "leftbutton")
GUI.skin.GetStyle(style.name + "rightbutton")
where style is the style you've given. So you have to set your skin as the current and make sure you have the additional styles as well.
Answer by Sundar · Aug 06, 2012 at 06:14 PM
Can you check in the inspector your chatSkin - Horizontal Scrollbar - Normal - Background - a texture should have been assigned to it or if you see "missing texture" then you need to assign a texture to the background. Also check the same for Vertical Scrollbar.
Both the Horizontal Scrollbar and the Vertical Scrollbar have an assigned texture :/
Just checking, do you have this line GUI.skin = chatSkin;
void OnGUI() {
GUI.skin = chatSkin;
.
.
.
}
I actually didn't have that line in my script, but even putting that before the "BeginScrollView", it outputed with the same error.
Though I don't think it would matter anyway, the chatSkin is assigned in the inspector, and the only thing the "BeginScrollView" requires are two styles, not an entire skin package.
Your answer
Follow this Question
Related Questions
Display "organized" string on GUI Label 1 Answer
GUI Error: You are pushing more GUIClips than you are popping 0 Answers
Drawing several BeginArea inside a BeginScrollview (GUILayout) produces an unexpected behaviour 1 Answer
Changing a GUIStyle for a button from Java Script to C# 2 Answers
Multiple Cars not working 1 Answer