Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Can anyone figure this out?

Discussion in 'Scripting' started by Josenifftodd, May 27, 2015.

  1. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Code (CSharp):
    1. public List<GameObject> deck = new List<GameObject>();
    2.     public List<GameObject> playerhand = new List<GameObject>();
    3.     public List<GameObject> oponentshand = new List<GameObject>();
    I have the deck, hand and opponents hand. I'm trying to make it so once the deck has shuffles it adds 6 cards to each deck, It does do this so far but it doesn't remove the prefabs from deck. Anyone know how to make it so it removes the cards it puts into Players-hand/op hand?

    I tried
    Code (CSharp):
    1. deck.Remove(playerhand [1]);
    didn't work it removed the wrong card...
     
  2. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    You should be more precise, what your problem is. So, did you debug which card you want to remove in the array and which card is removed. Do you want to remove the first card, so you should 0 instead of 1 for example.
     
  3. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    My problem is, it adds cards to the playershand but doesn't remove them from the deck and deck.Remove just removes it from the scene...

    I'm just trying to make it so 12 cards are shared between 2 players... Seems to be almost next to impossible lol
     
  4. WheresMommy

    WheresMommy

    Joined:
    Oct 4, 2012
    Posts:
    890
    Okay, so your problem is, if your card from deck is number 3 and is added to the players hand, it does not remove it from the deck, so the opponent can get number 3 too, right? Could you paste your script, where the cards are added to the players and opponents list?
     
  5. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Try something like:

    Code (CSharp):
    1. public List<GameObject> DrawCards(int number)
    2. {
    3.      List<GameObject> result = new List<GameObject>();
    4.  
    5.      for (int i = 0; i < number; i++)
    6.      {
    7.           if (deck.Count == 0)
    8.                break;
    9.  
    10.           int idx = deck.Count - 1;
    11.           GameObject card = deck[idx];
    12.           result.Add( card );
    13.  
    14.           deck.RemoveAt( idx );
    15.      }
    16.  
    17.      return result;
    18. }
    19.  
    20. void Start()
    21. {
    22.      playerhand.AddRange( DrawCards(6) );
    23.      oponentshand.AddRange( DrawCards(6) );
    24. }
     
  6. BenZed

    BenZed

    Joined:
    May 29, 2014
    Posts:
    524
    Just out of curiosity, if you're using GameObjects anyway, why use a list at all?

    You could have GameObjects to represent the Deck and the Player, and keep track of who has what changing the cards transform parent. If it's parented to the deck, it belongs to the deck. If it's parented to the player, it belongs to the player.
     
    Josenifftodd and Fajlworks like this.
  7. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    You raise a good point but I'm not sure how to do that... :/ but the idea I have is so simple to play :( dont wanna let anyone else do it too in case they say it is theirs... :/
     
  8. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Also I'm pretty sure the deck and the hand are on the same object at the moment how I've done it. I'm just not sure if I instantiate the hand it'll be the same cards that are on the scene if that makes sense as I'm not sure how id instantiate the hand lol.
     
  9. Fajlworks

    Fajlworks

    Joined:
    Sep 8, 2014
    Posts:
    344
    Don't get this wrong, don't want to bash or anything but:
    And yet at the same time you ask others to help you. ¬_¬

    Generally, ideas are not worth much; sure they are a great starting point and guide when working on a project, but it's the implementation and hard work that matters the most. I would suggest you download one of Unity tutorial projects, and analyse how they are structured. I'm sure you will benefit from them tremendously.

    Good luck!
     
    gavinb80 and StarManta like this.
  10. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    I mean making the actual game haha ! help is okay obviously but no point getting someone to do whole game if i can somehow myself. I'll look through but haven't come across anything card related thats useful to follow I've just been winging what I know so far... if you could point me in the right direction that would be awesome!
     
  11. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    instantiate your cards onto the deck as an gameobject. You can then child them to a "hand" gameobject using

    Code (csharp):
    1.  
    2. //instantiate the card then
    3. cardX.transform.parent = deck.transform;
    4.  
    5. // when you want it to go to a hand do
    6. cardX.transform.parent = handY.transform;
    7.  
    http://docs.unity3d.com/ScriptReference/Transform-parent.html

    I would probably keep a list of the card in the "deck" gameobject script so you can loop through and shuffle and distribute easier, and also loop through and set everything back to have the deck as parent at the end of the round/game/whatever

    you can then use

    Code (csharp):
    1.  
    2. GetComponentsInChildren<GameObject>();
    3. // or if you have non card children on the hands maybe have a class on the card and something like
    4. GetComponentsInChildren<Card>();
    5.  
    in the hand scripts to get references to the cards in the hand

    http://docs.unity3d.com/ScriptReference/Component.GetComponentsInChildren.html
     
  12. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Yeh thats what it does at the moment, it puts 52 cards onto the scene and if i move 1 card they will be another card under it, what you've put above has give me a rough idea of what you are saying just how would i make it so that when the hand is being set to a child of the playershand it only grabs 6 cards thats the main thing I don't get lol the stuff above is a huge help though man!
     
  13. Josenifftodd

    Josenifftodd

    Joined:
    Nov 29, 2013
    Posts:
    158
    Infact I only shown you a short snippet of my code.. lol i'll copy and paste it on here then you'll understand the question im asking better :)

    This is what I have so far::

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PlayersHand : MonoBehaviour {
    6.     public List<GameObject> deck = new List<GameObject>();
    7.     public List<GameObject> playerhand = new List<GameObject>();
    8.     public List<GameObject> oponentshand = new List<GameObject>();
    9.     public GameObject SpawnSpot;
    10.  
    11.  
    12.  
    13.     // Use this for initialization
    14.     void Start () {
    15.         ShuffleCards ();
    16.         AddCards ();
    17.  
    18.     }
    19.     // Update is called once per frame
    20.     void Update () {
    21.  
    22.     }
    23.  
    24.     public void ShuffleCards () {
    25.         GameObject Deck = GameObject.Find("Deck");
    26.  
    27.             for(int i=0;i<52;i++){
    28.                 int prefabIndex = UnityEngine.Random.Range(0,deck.Count -1);
    29.                 GameObject go = Instantiate(deck[prefabIndex], new Vector3 (0,0,0), Quaternion.identity) as GameObject;
    30.                 go.transform.parent = Deck.transform;
    31.                 go.transform.position = SpawnSpot.transform.position;
    32.             }
    33.     }
    34.  
    35.  
    36. void AddCards () {
    37.         // Player 1 Cards
    38.         playerhand.Add (deck [1]);
    39.         playerhand.Add (deck [3]);
    40.         playerhand.Add (deck [5]);
    41.         playerhand.Add (deck [7]);
    42.         playerhand.Add (deck [9]);
    43.         playerhand.Add (deck [11]);
    44.         // Player 2 Cards
    45.         oponentshand.Add (deck [2]);
    46.         oponentshand.Add (deck [4]);
    47.         oponentshand.Add (deck [6]);
    48.         oponentshand.Add (deck [8]);
    49.         oponentshand.Add (deck [10]);
    50.         oponentshand.Add (deck [12]);
    51.  
    52.     }
    53. }
    54.