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);
}
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.
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;
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