Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Feb 19, 2020 at 12:51 AM by tormentoarmagedoom for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Siggytron · Feb 18, 2020 at 06:39 PM · arraysindexoutofrangeexception

Very basic code yet IndexOutOfRangeException: Index was outside the bounds of the array.

 public class Button : MonoBehaviour
 {
     public Material[] mButton;
     Renderer rend;
 
     // Start is called before the first frame update
     void Start()
     {
         Debug.Log(mButton[0]);
         rend = GetComponent<Renderer>();
         rend.sharedMaterial = mButton[0];
     }

I am getting a "Index Out of Bounds" error at the rend.sharedMaterial=mButton[0] line. It is an array of two elements. I have different materials in each of the two locations of the array. The Debug. Log accurately shows which material is in the [0] location.

Any ideas? Thanks in advance.

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

2 Replies

  • Sort: 
avatar image
0
Best Answer

Answer by Siggytron · Feb 18, 2020 at 10:17 PM

So someone on a another forum asked me to double-check that the error wasn't coming from the Debug.Log line and indeed it was. When I comment out the Debug.log line, it clears the error. Thank you @tormentoarmagedoom for your assistance as well.

Comment
Add comment · Show 2 · 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 kensct · Feb 18, 2020 at 11:24 PM 1
Share

This probably means you have two copies of button in the scene. In hierarchy search fro Button it will show you all the GameObjects with that script assigned. Then you can make sure all the materials are assigned.

avatar image JPhilipp · Feb 18, 2020 at 11:33 PM 0
Share

@$$anonymous$$ensct Right that was my first thought too, at StackOverflow where the question was crossposted.

avatar image
1

Answer by tormentoarmagedoom · Feb 18, 2020 at 06:48 PM

Hello.

As you declare mButton but I dont see a value assigin code, i supose you assign it via inspector. Are you sure you defined the array and its elements? can you post a screenshot of the inspector to see it? Think that you need to assign the values for each buton script if you are doing it via inspector. there are more than 1.

By now, you can say only 1 thing is true. If error says IndexOutOfRangeException, is because index is higher than the number of elements. As you use the 0, it means the array is empty, so check why.

Bye!

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 Siggytron · Feb 18, 2020 at 09:08 PM 0
Share

You're right that I am assigning it via the inspector. Here is the screenshot of my inspector. (I changed the script name to 'Next').

(For some reason, the upload function keeps giving me a parsing error) I'll try to describe my inspector then.

 $$anonymous$$ Button 
 Size                 2 
 Element 0      $$anonymous$$aterial named 'Aqua' 
 Element 1      $$anonymous$$aterial named 'Pink'

Also, how would I assign materials to the array before I give the instruction to get the renderer? I tried assigning arbitrary integers when I declare the array, but Unity won't let me set up the array with integers and then have it switch over to holding materials.

Thanks.

Follow this Question

Answers Answers and Comments

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

IndexOutOfRangeException: Array index is out of range. on "frequency = frequencies[thisFreq];" 0 Answers

C# List index confusion using Mathf.Clamp 1 Answer

Error: IndexOutOfRange: call for index 0 in an array with length 1(+) 1 Answer

Array of Arrays 3 Answers

Array cannot read Greek characters 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