Search Unity

IndexOutOfRangeException: Index was outside the bounds of the array????

Discussion in 'Scripting' started by Night-Wing-Zero, Jul 22, 2019.

  1. Night-Wing-Zero

    Night-Wing-Zero

    Joined:
    Nov 30, 2017
    Posts:
    4
    Greetings Unity Community, Daniel speaking, I cant figure out why I have this error on my code on the console output, below here I'm sharing you my C# and the screenshot error that I'm getting asee whats going on to fix it. :)
     

    Attached Files:

  2. palex-nx

    palex-nx

    Joined:
    Jul 23, 2018
    Posts:
    1,171
    Obviously that's because your randomIndex do not fit length of the cards array.
     
  3. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    Where is tempCards initialized? If it's set in the inspector, are there at least 51 elements in it?

    If you want to figure out the answer and solve the problem I get that, though you should know that there are tons of C# array-shuffling algorithms with code a google search away.
     
  4. Night-Wing-Zero

    Night-Wing-Zero

    Joined:
    Nov 30, 2017
    Posts:
    4
    Well, how Do I solve this because truth is this very code that you saw is from a Online course that I am into
     
  5. Night-Wing-Zero

    Night-Wing-Zero

    Joined:
    Nov 30, 2017
    Posts:
    4
    This is my whole code, where Do i solve this the error that is giving me? Honestly if this the feedback that I am getting then why I even bother on learning unity since C# aint my strenght.

    I need help, that's all I am asking

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class BlackjackManager : MonoBehaviour{

    public float money = 100f, wager = 10.00f;

    int playerPoints, dealerPoints;

    public int[] cardValues, tempCardValues;

    public Sprite[] cards, tempCards;

    public bool[] ace, tempAce;



    // Start is called before the first frame update
    void Start() {

    }

    // Update is called once per frame
    void Update() {

    }



    // used for shuffling our deck
    void Shuffle() {
    Random.seed = (int)System.DateTime.Now.Ticks;

    for (int i = 0; i < cards.Length; i++) {
    int randomIndex = Random.Range(0, 51);

    while (cards != tempCards[randomIndex]){

    if (tempCards[randomIndex] == null){
    tempCards[randomIndex] = cards ;
    tempCardValues[randomIndex] = cardValues ;
    tempAce[randomIndex] = ace ;
    } else {

    if (randomIndex < cards.Length - 1)
    randomIndex++;
    else if (randomIndex == cards.Length - 1)
    randomIndex = 0;
    }
    }

    }

    for (int x = 0; x < cards.Length; x++) {
    cards [x] = tempCards[x];
    tempCards [x] = null;
    cardValues [x] = tempCardValues[x];
    tempCardValues [x] = 0;
    ace [x] = tempAce[x];
    tempAce [x] = false;
    }
    }

    }
     
  6. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    What I'm saying is that the answer to your problem is outside of the code. On line 12 you define two arrays, but they aren't initialized; there's nothing in them. This isn't a problem if you put items into these arrays somewhere else. If you look at this object in the Unity editor, you'll see two empty arrays. You can add items to these arrays in the inspector (and hopefully whatever tutorial you were following instructs you how to do this, otherwise it's a very bad tutorial*), and as long as you have at least 51 Sprites in the array, that would prevent the line from giving you the error.

    * Looking at the code, it doesn't look like it's a particularly good tutorial at least, so I'm not ruling out the possibility that it is indeed a bad tutorial.
     
  7. Night-Wing-Zero

    Night-Wing-Zero

    Joined:
    Nov 30, 2017
    Posts:
    4
    Strange, the tutoral that I'm following says to be made that way and the guy who codes it makes it work normally, UNLESS, the latest version of unity is no longer compatible with the scripting that I am making and was made with previous versions of Unity Itself (note: I am currently using Unity version 2019.3.0a7, maybe I should downgrade and see if I notice any changes, I'll keep you updated on this)
     
  8. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    6,528
    Nope, nothing I wrote there is version-dependent, that would be pretty much identical since Unity 1.x. You need to initialize the array and fill it up one way or another, either in code or in the inspector, there's no way around it.