• 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
-1
Question by PeteFL · Apr 22, 2018 at 05:37 PM · arrayindex

Array index is out of range

Apologies if there's already an answer to this, it's probably something obvious but I can't work it out. I'm getting an Array Index Out Of Range error on one of my lists.

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
1
Best Answer

Answer by TanselAltinel · Apr 22, 2018 at 06:23 PM

2 solutsions:

  1. Make answerObjects variable private

  2. Remove answ1, ans2, ans3, ans4 and assign them to answerObjects fields in the editor, and remove Start() function.

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 PeteFL · Apr 22, 2018 at 06:35 PM 0
Share

1 is giving me an error. Can I have an example of what the code would look like for #2 to initialise the list please?

avatar image TanselAltinel PeteFL · Apr 22, 2018 at 06:37 PM 0
Share

Code should be like this and objects answ1, ans2, ans3, ans4 should be assigned to answerObjects in the editor inspector area.

 public Text$$anonymous$$esh[] answerObjects;
  
  void Start () {
  }
  
  public void procedure() {
          //some preceeding code
      int correct = rnd.Next (0, 4);
      int incorrectIndex = 1;
      for (i = 0; i < 4; i++) {
          if (correct == i) {
              answerObjects [i].tag = "correct";
              answerObjects [i].text = answers [0].ToString ();
          } else {
              answerObjects [i].tag = "incorrect";
              answerObjects [i].text = answers [incorrectIndex].ToString();
              incorrectIndex += 1;
          }
      }
avatar image PeteFL TanselAltinel · Apr 22, 2018 at 06:48 PM 1
Share

Works perfectly, thanks!

Show more comments

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

91 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

Related Questions

List gives "Array index is out of range" for no reason 2 Answers

Null first array index and shift rest by 1? 1 Answer

Change an objects Array position dynamiclly in a match 3 game 0 Answers

playing a different animation in an array 1 Answer

array problem index out of range ? why ? 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges