(TIME SENSITIVE ) ArgumentOutOfRangeExeption

Hi, I’m doing my first game jam and im trying to instantiate certain GameObjects from a list when a button is pushed. However, I keep getting this error: ArgumentOutOfRangeExeption for the list of GameObjects. here is my code:

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

public class TilePlacementButtons : MonoBehaviour
{
public List availableCards = new List();
[SerializeField] public GameObject selectedCard;
int cardIndex = 0;
int startingIndex = 0;

void Start()
{
SetupCards();
}

private void Update()
{
CycleCards();
}

private void SetupCards()
{
int cardMax = availableCards.Count;
int cardMin = 0;
cardIndex = Mathf.Clamp(startingIndex, cardMin, cardMax);
}

private void CycleCards()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
cardIndex = +1;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
cardIndex = -1;
}
selectedCard = availableCards[cardIndex];

}

public void OnClick()
{
Instantiate(availableCards[0], transform.position, Quaternion.identity);
}
}

If anyone could help that would be exellent, but please be quick I have limited time as this is a game jam. Thanks!

What is the index value when you receive the exception? Tips for new Unity users I suspect it is -1 :slight_smile:

I’m pretty new to coding so how would I find the Index? If you mean which number in the list i’m trying to instantiate it’s 0

You would debug it as shown in the video. I meant the actual index value when you receive the exception. Debugging will provide this information. One note, the code should be cardIndex -= 1, I suspect you are trying to subtract 1 from the current value, but you are instead explicitly setting it to -1. Debugging will confirm.

Ok I tried Debug.Log for cardIndex and it stayed at 0 the whole time

Great, you are making progress. Why does it stay 0, etc. Please show your updated code with Debug.Log

It is supposed to stay 0. I’m getting some help from Discord and they reccomended moving the clamp on cardIndex to Update her is the code:
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TilePlacementButtons : MonoBehaviour
{
public List availableCards = new List();
[SerializeField] public GameObject selectedCard;
int cardIndex = 0;
int startingIndex = 0;

void Start()
{
SetupCards();
}

private void Update()
{
Debug.Log(cardIndex);
int cardMax = availableCards.Count;
int cardMin = 0;
cardIndex = Mathf.Clamp(startingIndex, cardMin, cardMax - 1);
CycleCards();
}

private void SetupCards()
{

}

private void CycleCards()
{
if (Input.GetKeyDown(KeyCode.RightArrow))
{
cardIndex += 1;
}
if (Input.GetKeyDown(KeyCode.LeftArrow))
{
cardIndex -= 1;
}
selectedCard = availableCards[cardIndex];

}

public void OnClick()
{
Instantiate(availableCards[0], transform.position, Quaternion.identity);
}
}

No, you are clearly changing cardIndex

I just noticed an error there in the clamp. When it’s in the update it constantly resets itself. I have fixed that

Yes the goal is to be able to change it but right now i’m just trying to instantiate anything at all from the list

What line is the error on?

Line 51

Clearly we need to see the code on line 51 :slight_smile:

That line is:
Instantiate(availableCards[0], transform.position, Quaternion.identity);

It’s supposed to be:
Instantiate(selectedCard, transform.position, Quaternion.identity);

but I have it changed for this debuging. I directly plugged in the first part of the list

Ok, so have you solved your problem? Otherwise, if you are getting that exception, the availableCards list is empty, and there is no object at position 0. Debug.Log it to confirm

5264312--526697--Screen Shot 2019-12-09 at 6.26.37 PM.png

That is the list in the inspector

Ok I fixed it thanks for your help