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 Abhi94 · Jun 12, 2015 at 06:06 PM · arrayscript error

My script seems to work fine , but i seem to get this error when i play

IndexOutOfRangeException: Array index is out of range.

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class DisplayColor : MonoBehaviour {
      
     public Button[] buttons;
 
 
 
 
 
 
 public    void Start () {
 
         changeColor ();
 
         }
 
 
 
 public    void changeColor() {
         GetComponent<Image> ().color = buttons [Random.Range (0, buttons.Length - 1)].image.color;
 
     }
 }




 
Comment
Add comment · Show 2
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 fafase · Jun 12, 2015 at 06:53 PM 0
Share

Your script is fine as is. $$anonymous$$ost likely, your array is empty and random returns 0 where there is no item in the array.

avatar image Abhi94 · Jun 13, 2015 at 06:17 AM 0
Share

Well fafase i have been facing this problem since i have tried calling changeColor from other script , i am attaching that script as well,

  using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class onClick1 : $$anonymous$$onoBehaviour {
 
     public Text ScoreText;
     private int Count = 10;
     private int wrongAnswer = -5;
     public bool hello;
 
     public    void SayHello(  bool hello = false ) {
 
         
       if (GetComponent<Image> ().color == GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
 
             GameObject thePlayer = GameObject.Find("DisplayButton");
             DisplayColor displayColor = thePlayer.GetComponent<DisplayColor>();
         
 
             hello = true;
             displayColor.changeColor();
             ScoreText.text = "SCORE: " + Count;
 
         
         
         
         } else if (GetComponent<Image> ().color != GameObject.FindGameObjectWithTag ("DisplayButton").GetComponent<Image> ().color) {
     
             hello = false;
             ScoreText.text = "SCORE: " + wrongAnswer;
         
         }
         }
     
 }

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by FortisVenaliter · Jun 12, 2015 at 06:38 PM

Okay, first off, please use more descriptive titles for your questions in the future, if you want good replies. Secondly, if your code is throwing that exception, then it is not working fine.

Third, this is a basic programming problem, not usually allowed on this site. You need to look up the IndexOutOfRangeException on MSDN and learn what it means. Then you need to add Debug.Log() calls to your code to output the actual values and compare that to what you expect. Once you've found the difference, you find where the code is going wrong to create that difference, and correct it to get the expected result. This procedure is called debugging.

Also, in the future, you'll want to post screenshots of your hierarchy and scene view. Most likely, your problem is not defining the buttons array in Unity, but I have no idea if that's the case, because you included no explanation in your question.

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 fafase · Jun 12, 2015 at 06:46 PM 1
Share

well, this is no real answer to the question, more of a guideline. Better off as a comment.

avatar image Abhi94 · Jun 13, 2015 at 06:14 AM 0
Share

well ty fafase , and fortis i would ask any question that i want , and if you want to answer then help or otherwise don't . Because thats not an answer its just a procedure

avatar image FortisVenaliter · Jun 13, 2015 at 01:24 PM 0
Share

It's a procedure that's incredibly important to game development. Sorry if I came across as harsh; I certainly could have worded it better. But if you don't learn the process, you're going to end up with more frustration in the long run. It goes back to the whole "give a man a fish"/"$$anonymous$$ch him to fish" thing (although, admittedly, my answer is not $$anonymous$$ching). If you have the resources and skills to figure out the problems yourself, you'll find program$$anonymous$$g more rewarding.

avatar image
0

Answer by tanoshimi · Jun 13, 2015 at 07:28 AM

Your problem is that, when Buttons is an empty array, Random.Range (0, buttons.Length - 1) will return a value between 0 and -1, and you can't access the -1ᵗʰ element of an array...

Since Random.Range is exclusive of the top integer value (docs), you should use Random.Range (0, buttons.Length), and also make sure that the buttons array actually has some elements in it.

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 Abhi94 · Jun 13, 2015 at 09:35 AM 0
Share

Thankyou guys for your help , I have figured my mistake , it was quite foolish i forgot to delete an empty gameObject .

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

2 People are following this question.

avatar image avatar image

Related Questions

Problem with arrays and accessing sriptable objects. 2 Answers

Creating an array of materials in a GameController script 0 Answers

List and store vector3 of game objects with tag 1 Answer

jagged array in c# from different sized units 1 Answer

Multiple collision array 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