Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by CrashDavis · Jul 22, 2014 at 09:43 PM · c#arrayvector3basic programming

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...

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

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image robertbu · Jul 22, 2014 at 10:52 PM 0
Share

Usually this problem is solved by shuffling the cards and then dealing them out in order. No need to remove anything from the array.

http://answers.unity3d.com/questions/16531/randomizing-arrays.html

avatar image CrashDavis · Jul 23, 2014 at 02:12 PM 0
Share

You know, I've seen the fisher-yates thing everywhere. I never understood it until now: the method described above is like pulling cards randomly out of a hat, not replacing, until you have a randomly ordered deck. FY is just shuffling (like normal poker-style shuffling). Either way, the end result is the same: a randomly ordered deck. brain-splosion.

Thanks robertbu.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by SirCrazyNugget · Jul 22, 2014 at 11:07 PM

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[i], Quaternion.identity);
             playDeck.RemoveAt (findCardID);
             Debug.Log (string.Format ("Placed card: {0} of {1} at {2}", findCard.rank, findCard.suit, positions[i]));
         }
 
     }








Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image CrashDavis · Jul 23, 2014 at 02:28 PM 0
Share

Somewhat?! This is awesome! Dude, thanks SO much!

It's funny how the simplest things in life are SO convoluted in math.

:/

Hmm. I don't understand all of the code...but you can bet I'll figure it out.

Borderline obnoxious: does anyone have any good sites for learning code? Something you stumbled upon and are really thankful to have found? There's so much out there but not all of it is clear. I like codeAcademy myself...

Anyway. Thanks again SirCrazyNugget!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

Related Questions

What is the order/connection of array elements in mesh.vertices? 2 Answers

c# : Accessing Struct variables inside an Array 1 Answer

Read variable from txt to Vector3 1 Answer

instantiate problem help? 1 Answer

Strange behaviour with List C# 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges