IndexOutOfRangeException

Having a little trouble seeing how to fix this small problem. Basically when I click an object it returns the object name, then based on the objects name it builds an array to then go add said object to a deck of cards. The reason it is an array is the method for AddToDeck() uses an array as it initially builds a deck of cards based on an array from a text file.

I’m sure it is a small error but currently I’ve been staring at the code for so long it eludes me. Would appreciate the help.

public void OnCardHandClick(string cardName)
	{
		Debug.Log ("You have clicked a card");
		string[] temp = {};
		if(cardName == "Battle Chariot")
		{
			Debug.Log ("You have entered the if statement");
			temp[0] = "1";
			Debug.Log(temp);
			playerActive.AddToDeck(temp);
			Debug.Log ("What is in the playerActive deck");
			Debug.Log(playerActive.deck);
		}
		
		//obj.transform.parent = playerActive.transform;
	}

You are creating an empty string array (i.e. it has 0 elements), so you can’t say “temp[0]” to access the first element, there is no first element.

If you just want 1 element in the array, do it like this:

string[] temp = new string[1];

Then you can access string[0].