Quantcast
Channel: Questions in topic: "error message"
Viewing all articles
Browse latest Browse all 2891

StackOverflowException, cant find out how to fix it

$
0
0
Hi everyone! I keep getting the following error. Ive tried changing my code in several places but I seem unable to figure out where it actually goes wrong, so I hope someone can help me out here :) StackOverflowException: The requested operation caused a stack overflow. Deck.DrawACard () (at Assets/Code/Cards/Deck.cs:144) Deck.DrawACard () (at Assets/Code/Cards/Deck.cs:144) etc.. (removed the rest for readability) Deck.Draw My code is as follows: public void DrawACard() { float randomValueFloat; //Roll a random value between 0 and if (cardsInDeck -1 > 0) { randomValueFloat = Random.Range(0, cardsInDeck - 1); } else { randomValueFloat = 0; } //Convert randomvalue to int int randomValue = (int)randomValueFloat; //Set the index of what cardSlot in the deck is being checked to 0 int cardIndex = -1; //For loop: As long as i is smaller than or equal to the randomvalue that has been rolled for (int i = 0; i <= randomValue;) { //cardIndex +=1 (cardIndex is to check which card in the lists to check) cardIndex++; if (cardsInHand >= cardsInDeck) { return; } //If the currently checked cardSlot isnt empty else if (cardDeck[cardIndex] != null) { //if i is equal to the randomValue(1) and the currently checked card isnt drawn (1): randomValue determines what card should be drawn from the number of occupied cardSlots in the deck if (i == randomValue && isCardDrawnList[cardIndex] == false) { //Sets the card to drawn isCardDrawnList[cardIndex] = true; //Insantiates the new card as newlyDrawnCard at the position of the slot that its drawn in GameObject newlyDrawnCard = Instantiate(cardDeck[cardIndex], new Vector3(drawSlot.transform.position.x, drawSlot.transform.position.y, 0), Quaternion.identity); //With the proper scale newlyDrawnCard.transform.localScale = new Vector3(0.9f, 0.9f, 1); //Saves the cardIndex of this card in the card (so that this isCardDrawn can be set to false again when the card is played) newlyDrawnCard.GetComponent().cardIndexDrawn = cardIndex; //Sets the slotOccupying of this card to be the slot it was drawn in newlyDrawnCard.GetComponent().slotOccupying = drawSlot; //Ends the function return; } else if (i == randomValue && isCardDrawnList[cardIndex] == true) { //Rerolls the entire function DrawACard(); //Ends this instance of the function return; } //i += 1, because a card has been found to check i++; } } } I also create a list in start containing 30 public GameObjects and a list that contains 30 bools wether the card has been drawn. cardsInDeck is also determined in start with the following code: //go by all the cardSlots in the deck for (int i = 0; i < cardDeck.Count; i++) { //Checks if the cardSlot isnt empty (!= null) if (cardDeck[i] != null) { //Adds 1 to cardsInDeck cardsInDeck += 1; } } I dont see how Im getting a StackOverflow error when the card shouldnt even infinitely be going to DrawACard() Hope someone can help. Thanks!

Viewing all articles
Browse latest Browse all 2891

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>