How to randomly access Vector3's from an array?

Hi! So I’ve been trying to figure this out for 2 days now; it’s time to ask for help.

My goal: Create simple memory card game (the kind where you try to flip over two of the same cards etc…)

What I’m stuck on: Trying to get the cards to spawn randomly every game.

Here’s what I thought I’d do:

using UnityEngine;
using System.Collections;

public class RandomCardLocations : MonoBehaviour
{
  public GameObject card1;
  Vector3[] cardLocations = {Vector3 (-1.8, 0, 0), etc...};

  void Start()
  {
    for (int i = 0; 1 < 10; i++)
    {  
      float number = Random.Range(0F, cardLocations.length);
      Debug.Log (number);
      Instantiate(card1, cardLocations[number]);
      remove.cardLocations[number]; 

    }
  }
}

The basic idea is that there is an array with 20 some-odd Vector3 positions. A random number generator picks one of these positions randomly, spawns the object there, then removes that location from the array so it can’t be picked again.

The random number generator is bound by the length of the array, so you will always be pulling from the right space (won’t get “array.position[456]” when there are only 3 positions left).

List of my problems:

  1. Code Retarded (ID10T Error code [idiot] xD)

  2. Vector 3 array won’t work. Like, at all. What am I doing wrong? I’m working on a Surface Pro, but that shouldn’t have any effect, should it? More specifically, when I create any kind of array, no matter where, Mono says that ‘void start’ should be removed. WHAT?!

  3. How to remove something from an array?

  4. Once I get the for-loop and array up and running, the way the code is now it will spawn the same object at all locations (in a random order. GREAT). Any suggestions?

  5. Can’t pay for college. Can you help? Oh, wait, wrong forum…

:smiley: So yeah. Any help at all, monetary or otherwise, is welcome and greatly needed. Anyone’s two cents are appreciated.

A little more than your initial question but hopefully will help you out somewhat. Depending on how you’re generating the deck will determine the best way of setting it up but I’ve created one randomly just for demonstration purposes so you’ll need to either place the Card class (script) onto each playable card then use GetCompomentsInChildren to find the cards and store them in the fullDeck or allocate them manually, make Rank and Suit members of the Card Serialized Fields and used a properties to return these values.

The basic concept is once you have a fullDeck defined, drawDeck clones this deck to enable restarting done easily. n/2 cards are chosen from the drawDeck and placed into the playDeck (twice each) to give you the random deck you’ll be selecting pairs from. If you’re drawing the same n/2 cards each time you can skip this process. A random card is then placed from the playDeck in each position. You can then check combinations using rank and suit to verify if they match the other cards.

I may have slightly over complicated what you were after but hopefully will give you a nudge in the general direction for what you’re after. (And obviously you’ll need to break out these classes in separate scripts to be able to use them as components on GameObjects.

	public enum Suits {
		Hearts,
		Clubs,
		Diamonds,
		Spades
	}

	public enum Ranks {
		Ace,
		Two,
		Three,
		Four,
		Five,
		Six,
		Seven,
		Eight,
		Nine,
		Ten,
		Jack,
		Queen,
		King
	}

	public class Card : MonoBehaviour {

		public Suits suit { get; private set; }
		public Ranks rank { get; private set; }

		public Card(Suits s, Ranks r){
			suit = s;
			rank = r;
		}
	}

	public int memoryCardsMax = 8;

	List<Card> fullDeck; //populated once on start
	List<Card> drawDeck; //cloned from fullDeck at the beginning of the game
	List<Card> playDeck; //stores the cards which are drawn (*2) from drawDeck

	Vector3[] positions; //array of the locations where cards are placed

	void Start(){

		//store the available placement position
		positions = new Vector3[]{
			new Vector3 (-1.8f,  0, 0),
			new Vector3 (    0,  0, 0),
			new Vector3 ( 1.8f,  0, 0),
			new Vector3 ( 3.6f,  0, 0),
			new Vector3 (-1.8f, 4f, 0),
			new Vector3 (    0, 4f, 0),
			new Vector3 ( 1.8f, 4f, 0),
			new Vector3 ( 3.6f, 4f, 0)
		};

		//create the deck (this is based on a normal playing deck of 52 cards)
		fullDeck = new List<Card>();
		foreach (Suits suit in (Suits[]) System.Enum.GetValues (typeof(Suits))){

			foreach(Ranks rank in (Ranks[]) System.Enum.GetValues (typeof(Ranks))){

				fullDeck.Add (new Card(suit, rank));
			}
		}

		BeginGame ();
	}

	void BeginGame(){

		//create a deck to draw cards from
		drawDeck = new List<Card>(fullDeck);
		playDeck = new List<Card>();
		for(int i = 0; i < memoryCardsMax; i += 2){

			//select a random card position
			int drawnCardID = Random.Range (0, drawDeck.Count);

			//select a random card from the draw deck
			Card drawnCard = drawDeck[drawnCardID];

			//add card to play deck (twice as they need to be able to match!)
			playDeck.Add (drawnCard);
			playDeck.Add (drawnCard);

			//remove it from the draw deck so it can't appear more again (other than its matched card)
			drawDeck.RemoveAt (drawnCardID);
		}

		//quick debug of all drawn cards
		foreach(Card card in playDeck){
			Debug.Log (string.Format ("Cards to place: {0} of {1}", card.rank, card.suit));
		}

		Debug.Log (drawDeck.Count);

		//place the cards in random position
		for(int i = 0; i < memoryCardsMax; i++){

			int findCardID = Random.Range (0, playDeck.Count);
			Card findCard = playDeck[findCardID];
//			Instantiate (findCard, positions*, Quaternion.identity);*
  •  	playDeck.RemoveAt (findCardID);*
    

_ Debug.Log (string.Format (“Placed card: {0} of {1} at {2}”, findCard.rank, findCard.suit, positions*));_
_
}*_

* }*