x


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: 125chatSkin from inspector125

125The chatSkin assigned in the inspector125

untitled-4.png (16.9 kB)
more ▼

asked Aug 06, 2012 at 04:53 PM

Freaking-Pingo gravatar image

Freaking-Pingo
366 29 30 36

(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

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

more ▼

answered Aug 06, 2012 at 06:58 PM

Seth Bergman gravatar image

Seth Bergman
8.5k 33 33 51

I believe the problem stems from trying to use a GUISkin, where a GUIStyle is required, not very familiar with GUIStyles though..

Aug 06, 2012 at 07:06 PM Seth Bergman

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)

Aug 06, 2012 at 07:07 PM Freaking-Pingo

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..

Aug 06, 2012 at 07:36 PM Seth Bergman
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 06, 2012 at 07:03 PM

Bunny83 gravatar image

Bunny83
74.6k 25 91 319

(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Aug 06, 2012 at 06:14 PM

Sundar gravatar image

Sundar
1.3k 3 3 5

Both the Horizontal Scrollbar and the Vertical Scrollbar have an assigned texture :/

Aug 06, 2012 at 06:32 PM Freaking-Pingo

Just checking, do you have this line GUI.skin = chatSkin;

void OnGUI() {

 GUI.skin = chatSkin;

  .
  .
  .

}

Aug 06, 2012 at 06:45 PM Sundar

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.

Aug 06, 2012 at 06:52 PM Freaking-Pingo
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x9432
x5621
x735
x171
x19

asked: Aug 06, 2012 at 04:53 PM

Seen: 1661 times

Last Updated: Aug 06, 2012 at 07:36 PM