- Home /
Help with for loop and arrays
Hello so i have a script that is suppose to spawn a card at a random cardSpot. So i tried to get all objects from an array in this case the cardSpots. And then check if the cardspots bool 'isFree' is true. Then i spawn a random card from an other array at that chosen cardspots position. But i get an error that says 'Index out of range' i checked the script and everything seems to be fine. Would be glad if someone could tell me what the issue is and why. Im sorry if it's really easy im new to loops and arrays! Anyhow thanks!
~flux
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Deck : MonoBehaviour {
public GameObject[] cards;
public GameObject[] cardSpots;
public void GiveCard ()
{
Debug.Log ("function worked");
for (int i = 0; i < cardSpots.Length; i++)
{
if (cardSpots [i].GetComponent<Spot> ().isFree == true)
{
Instantiate(cardSpots[Random.Range(0, cards.Length)], cardSpots[i].transform.position, Quaternion.identity);
cardSpots [i].GetComponent<Spot> ().isFree = false;
return;
}
}
}
}
Answer by HarshadK · May 11, 2017 at 02:22 PM
In your Instantiate you are trying to get an item from cardSpots by generating a random number which is between 0 and length of cards array. So since your cards array has length larger than cardSpots you are getting that error. Your Instantiate statement should be:
Instantiate(cardSpots[Random.Range(0, cardSpots.Length)], cardSpots[i].transform.position, Quaternion.identity);
Purely judging by the variable names, I think he wants to instantiate a card from cards-array into a cardspot, not the other way around so
Instantiate(cards[Random.Range(0, cards.Length)], cardSpots[i].transform.position, Quaternion.identity);
Answer by phxvyper · May 11, 2017 at 03:36 PM
In the line
Instantiate(cardSpots[Random.Range(0, cards.Length)], cardSpots[i].transform.position, Quaternion.identity);
You are basing the index you're getting in Random.Range(0, card.Length)
off of the cards
array, not the cardSpots
array. This wouldn't be problematic if both arrays had the same number of items in them, but clearly they have a different number of items.
Is your intention to get a GameObject from cards
or from cardSpots
? Make sure you're only basing your index off of the right array's length, not a different one.
For example:
Instantiate(cardSpots[Random.Range(0, cardSpots.Length)], cardSpots[i].transform.position, Quaternion.identity);
or:
Instantiate(cards[Random.Range(0, cards.Length)], cardSpots[i].transform.position, Quaternion.identity);
A really good way to stop confusion with variables is to more properly describe what they're doing. For example, if you're variable is intended to be a collection of Prefabs - not just game objects - call it CardPrefabs
instead of just Cards
, et cetera.
I had two errors and this was one of them, a typo thank you for your answer!
Answer by leech54 · May 11, 2017 at 03:20 PM
Random.Range(0, cards.Length) does not include the 0 but does include the cards.Length I think that makes it go out of bounds.
You can do
int i = Random.Range(0,cards.Length);
Instantiate(cards[i-1]),...)
from the Unity manual Note that max is inclusive, so using Random.Range( 0.0f, 1.0f ) could return 1.0 as a value.
Read carefully the documentation :
public static int Range(int $$anonymous$$, int max); Description
Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only).
Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9. If max equals $$anonymous$$, $$anonymous$$ will be returned.
I guess it matters if you use floats or int. It is done different.
public static float Range(float $$anonymous$$, float max);
Description
Returns a random float number between and $$anonymous$$ [inclusive] and max [inclusive] (Read Only).
Note that max is inclusive, so using Random.Range( 0.0f, 1.0f ) could return 1.0 as a value.
vs
public static int Range(int $$anonymous$$, int max);
Description
Returns a random integer number between $$anonymous$$ [inclusive] and max [exclusive] (Read Only).
Note that max is exclusive, so using Random.Range( 0, 10 ) will return values between 0 and 9. If max equals $$anonymous$$, $$anonymous$$ will be returned.
Your answer
Follow this Question
Related Questions
Unity C# Highscore table 2 Answers
Linear Interpolation of Array Values 1 Answer
Get all GameObjects by variable value 2 Answers
using arrays to perform multiple tasks 1 Answer
Problems with Arrays 2 Answers