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 TheCakeMaker · May 05, 2014 at 12:39 PM · nullreferenceexceptionarraysindexoutofrangeexception

Attempt at creating an array of text files results in different errors

Hello. I've been attempting to create an array based off of text objects and display them as buttons on screen. However when creating the buttons I would get a null reference exception error and I dont understand why this is happening.

Debug.log correctly indicates the current position in the array but upon displaying them within the scene Unity gives off an error. Any idea on what I'm doing wrong and any way to fix this? I tried to use a List instead but I wouldn't be able to load in the text files.

 public class QuestionPackSelection : MonoBehaviour {
     public TextAsset[] texts;
     GUIStyle PackGui;
     public int numberofquestionpacks;
     // Use this for initialization
     void Start () {
         texts = new TextAsset[numberofquestionpacks];
         texts = Resources.LoadAll<TextAsset>("QuestionPacks");
 
         for( int i = 0; i <= texts.Length; i++) {
             Console.WriteLine (texts[i].name); // results in IndexOutOfRangeException
             GUI.Button(new Rect(Screen.width - 200, 80+(55*i), 120, 50),texts[i].name.ToString(),PackGui); // results in NullReferenceException
         }
     }
 }


Edit: I tried editing the numberofquestionpacks and replaced it with 3 and 0 but no luck either.

Comment
Add comment · Show 7
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 $$anonymous$$ · May 05, 2014 at 02:06 PM 0
Share
  1. make sure that you have placed your "QuestionPacks" folder in the "Resources" folder.

  2. use for the loop: i < text.Lenght ins$$anonymous$$d of i <= text.Lenght.

  3. to see the Button you have to draw the Button more than one time -> use the OnGUI void.

avatar image TheCakeMaker · May 05, 2014 at 02:16 PM 0
Share

I already tried all 3 of those. Questionpacks are placed correctly otherwise Debug.log(texts[i].name); wouldnt work.

I swapped it to the OnGUI method as well and still the same errors, except this time they show up constantly.

Exact error is the following: NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.Button (Rect position, System.String text, UnityEngine.GUIStyle style) (at C:/BuildAgent/work/d3d49558e4d408f4/artifacts/EditorGenerated/GUI.cs:384) QuestionPackSelection.OnGUI () (at Assets/QuestionPackSelection.cs:20)

avatar image roojerry · May 05, 2014 at 02:31 PM 0
Share

Your GUIStyle is never set to anything, so it will always be null.

avatar image TheCakeMaker · May 05, 2014 at 02:37 PM 0
Share

yes, it will always be null and should just restore itself to default.

removing it has no effect on the errors so im assu$$anonymous$$g its an issue with the array

avatar image roojerry · May 05, 2014 at 02:46 PM 0
Share

What does your code look like now with the GUIStyle removed? Because if you are getting it to print the names in that for loop, it should have no problem displaying them in the buttons.

Also, an unassigned GUIStyle won't just be a default value, it will be null, and you should be getting a compiler warning alerting you to this.

Show more comments

1 Reply

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

Answer by Wolfram · May 05, 2014 at 06:57 PM

Is your problem resolved? According to your comments, that is unclear.

Concerning your original script:

  • reassigning "texts" to what Resources.LoadAll returns will simply overwrite your array, so the statement before that ("texts = new TextAsset[numberofquestionpacks];") is useless and can be removed.

  • Your for-loop goes from i == 0 to i == texts.Length, which is one too many. Always use "i < someArray.Length", instead of "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 TheCakeMaker · May 05, 2014 at 08:22 PM 0
Share

I guess my issue is finally solved. Interesting. I restarted unity and everything was fine. Null exception error gone.

Thanks.

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

22 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

Related Questions

C#: Arrays inside Arrays create a NullReferenceExeption? 1 Answer

IndexOutOfRangeException when activating a component(follow-up question) 1 Answer

HELP!!! This NullReferenceException won't go away! :( 1 Answer

NullReferenceException error 1 Answer

NullReferenceException - Array of Transforms? 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