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 iamthecoolguy11 · Oct 16, 2014 at 10:39 PM · c#guiinventorynull

How do I fix this null problem?

I am making a simple inventory and im trying to make it not do anything if the array is empty, but when it looks to see if it is null it says that it cant check because it is null. How do I fix this?.

here's part of the script

   public GameObject[] Slots;
 
     void OnGUI()
         {
         GUI.skin.font = font; 
     
         int offset = 0;
         for (int x = 0; x <= 30; x++) { 
             if(Slots[x]!= null){
                 if (GUI.Button (new Rect (10 + offset, 140, 60, 60), Slots[1].name))
                 {    
                     Debug.Log(x); 
                 }
                 offset += 65;
             }
         }       


here is the error

 IndexOutOfRangeException: Array index is out of range.
 ObjectHolder.OnGUI () (at Assets/Scripts/ObjectHolder.cs:37)






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

1 Reply

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

Answer by Habitablaba · Oct 16, 2014 at 10:45 PM

Wups. I got cocky and solved the wrong problem. Check this part out:

 for (int x = 0; x <= 30; x++)


There are two things here. One is mildly alarming, and the other is your problem.
First, don't count to x<=30, count to x < 30. This is where you're seeing your problem. If your array is 30 big, the last valid index is 29 (since arrays are 0 based).

The mildly alarming part is that you have 30 hard coded in here. Instead, you should use the .Length of your array. This way, if your array changes size later, you won't have to change your code.

Try:

 for (int x = 0; x < Slots.Length; x++)

Comment
Add comment · Show 4 · 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 iamthecoolguy11 · Oct 16, 2014 at 11:05 PM 0
Share

i still get the error lol

avatar image Habitablaba · Oct 16, 2014 at 11:34 PM 0
Share

Also, you are assu$$anonymous$$g there is more than one entry into the array on line 10 when you do Slots[1] (again because the first index is 0, not 1).

avatar image iamthecoolguy11 · Oct 16, 2014 at 11:36 PM 0
Share

alright im going to see if this work after im done with dinner lol but I thumbed it up for the help :)

avatar image iamthecoolguy11 · Oct 17, 2014 at 12:09 AM 0
Share

alright thanks man that worked

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

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

Related Questions

Is there any easy way to keep buttons inside of box? (c#) 1 Answer

Inventory Cursor Location 1 Answer

Multiple Cars not working 1 Answer

Using an On-Screen GUI Button to show a GUI? 1 Answer

Method is called, but GUI doesn't show up 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