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 waterdog · Apr 30, 2014 at 09:40 AM · c#arrayinspectorguistyleguiskin

Why does new GUIStyle() give "object reference not set to an instance of an object" error?

I have a GUISkin (buttonGUISkin) initialized in the inspector with a Custom Style named butGUIStyle. I want to create several unique GUIStyle instances using new GUIStyle() repeatedly with the GUIStyle from my GUISkin. I've used this basic structure successfully before. (See one of my other questions for more details) Before I had issues with naming Custom Styles. It seems the last time I updated Unity that problem went away and the Custom style name field can be used. But, now I get 'not set to an instance of an object' when using the same technique I, in fact, am using successfully elsewhere. Here is a code segment that fails, even when using "" as described in my previous question:

 using UnityEngine;
 using System.Collections;
 
 public class guiskintest : MonoBehaviour {
     
     private GUIStyle[] btnGUIArray;
     
     public GUISkin buttonGUISkin;
     
     private    bool firstpass = true;
     
     public int pttcnt=4;
     
     void OnGUI () {
         
         if (firstpass) {
 
             for (int i=0; i<pttcnt; i++) {
                btnGUIArray[i] = new GUIStyle(buttonGUISkin.GetStyle("butGUIStyle"));
             }    
 
             firstpass = false;
         }
     
     }
     
 }


Any thoughts on what is going on would be appreiciated!

Comment
Add comment · Show 3
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 maddFrogg · Apr 30, 2014 at 09:59 AM 0
Share

Are you sure your btnGUIArray is at least 4 units long? And even more, every of those 4 units has a value? Check it, it seems your btnGUIArray has a size of 0 or its values are null.

avatar image Nerevar · Apr 30, 2014 at 10:03 AM 0
Share

Hi

What do you exactly pass in your inspector to buttonGUISkin? a GUIStyle or GUISkin?

regards

avatar image waterdog · Apr 30, 2014 at 05:01 PM 0
Share

I just noticed and then saw maddFrogg's comment, too, that I likely forgot to actually initialize the array! Doh! I'll check for sure when I get home, but, that's probably it. I should sleep now and again...;^)

Nerevar, in the inspector buttonGUISkin contains a GUIStyle in the Custom Style section named butGUIStyle.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by wibble82 · Apr 30, 2014 at 05:45 PM

btnGUIArray is null!

inside the if(firstpass), you need to add btnGUIArray = new GUIStyle[pttcnt]

Comment
Add comment · Show 1 · 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 waterdog · May 01, 2014 at 01:39 AM 0
Share

Yep. As I noticed after finally getting 4 hours of sleep and as pointed out by maddFrogg and wibble82, I forgot to initialize the array...;^) Rather than include that in the if(firstpass) I added it in a Start() anticipating what I need to do in the final project:

 void Start () {
   btnGUIArray = new GUIStyle[pttcnt]; // GUIStyle array for styles from GUI skin
 } 

Thanks! Now if someone could just provide some insight to my quaternion question...?...;^)

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why can't I access the Name field in the Custom Styles section of a GUISkin in the Inspector? 2 Answers

Multidimentional Array for Inventory style system in inspector. 1 Answer

Array not updating in inspector 0 Answers

C# Array doesn't appear on Inspector 1 Answer

Development of the shop? 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