Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Freaking-Pingo · Aug 06, 2012 at 04:53 PM · c#guinullreferenceexceptionguistylebeginscrollview

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)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Seth-Bergman · Aug 06, 2012 at 07:06 PM 0
Share

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

avatar image Freaking-Pingo · Aug 06, 2012 at 07:07 PM 0
Share

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)

avatar image Seth-Bergman · Aug 06, 2012 at 07:36 PM 0
Share

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

avatar image
2

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Freaking-Pingo · Aug 06, 2012 at 06:32 PM 0
Share

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

avatar image Sundar · Aug 06, 2012 at 06:45 PM 0
Share

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

void OnGUI() {

  GUI.skin = chatSkin;

   .
   .
   .

}

avatar image Freaking-Pingo · Aug 06, 2012 at 06:52 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges