Hi I am new to Unity and trying to make a simple card game but have no idea how instantiate the cards. Basically, I am trying to give one card to the player hand and one card to the dealer hand.
Please help me writing this script.
Thank you
public Sprite[] cardSprites;
public GameObject cardPrefab;
public GameObject[] playerHand;
public GameObject[] dealerHand;
int[] cardValues = new int[53];
int currentIndex = 0;
public int DealCard(Card cardscript)
{
cardscript.SetSprite(cardSprites[currentIndex]);
cardscript.SetValue(cardValues[currentIndex]);
currentIndex++;
return cardscript.GetValueOfCard();}
if that’s your full code you have a long way to go - otherwise more code will be needed
what you’ll want to do:
- set the values of all your cards (set each cardValues[ ] value and cardSprites[ ])
- shuffle the deck - randomize them with some sort of swapping algorithm
- call DealCard as you do
from my understanding you need help with (1) - there’s two ways you can go about it
a) set the values in the Editor’s Inspector - potentially easier for loading sprites
b) do it with code - more consistent and better overall
my code is a bit rusty but you’ll want to do something like
public void Start()
for(int i = 0; i<53; i++)
{
cardValues[i] = i;
cardSprites[i].sprite = Resources.Load("card" + i);
}
Well my Shuffle in My DeckScript and My value in my Card Script. I am having problem bring it together
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DeckScript : MonoBehaviour
{
public Sprite[ ] cardSprites;
public GameObject cardPrefab;
public GameObject[ ] playerHand;
public GameObject[ ] dealerHand;
int[ ] cardValues = new int[53];
public List[ ] players;
int currentIndex = 0;
private List Player = new List();
// Start is called before the first frame update
void Start()
{
players = new List[ ] { Player };
GetCardValue();
}
// Update is called once per frame
void GetCardValue()
{
}
public void Shuffle()
{
for (int i = cardSprites.Length - 1; i > 0; --i)
{
int j = Mathf.FloorToInt(Random.Range(0.0f, 1.0f) * cardSprites.Length - 1) + 1;
Sprite face = cardSprites*;*
cardSprites = cardSprites*;*
cardSprites[j] = face;
int value = cardValues*;*
cardValues = cardValues[j];
cardValues[j] = value;
}
}
public int DealCard(Card cardscript)
{
cardscript.SetSprite(cardSprites[currentIndex]);
cardscript.SetValue(cardValues[currentIndex]);
currentIndex++;
return cardscript.GetValueOfCard();
}
public Sprite GetCardBack()
{
return cardSprites[0];
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Card : MonoBehaviour
{
public int value = 0;
public int GetValueOfCard()
{
return value;
}
public void SetValue(int newvalue)
{
value = newvalue;
}
public string GetSpriteName()
{
return GetComponent().sprite.name;
}
public void SetSprite(Sprite newsprite)
{
gameObject.GetComponent().sprite = newsprite;
}
public void ResetCard()
{
Sprite back = GameObject.Find(“DeckController”).GetComponent().GetCardBack();
gameObject.GetComponent().sprite = back;
value = 0;
// I am trying to grab the sprite reference out of the card script for my deckscript.
cardscript.gameObject.GetComponent().sprite = sprite;
//Set that sprite to deck script
for (int i = 0; i < 53; i++)
{
cardValues = i;
cardSprites*.sprite = Resources.Load(“card” + i);*
}
// And give a card to the dealer position and player position.
GameObject playerCard = Instantiate(cardPrefab, new Vector3(playerHand .transform.position.x, playerHand.transform.position.y), Quaternion.identity, playerHand.transform);
GameObject dealerCard = Instantiate cardPrefab, new Vector3(dealerHand .transform.position.x, dealerHand.transform.position.y), Quaternion.identity, dealerHand.transform);
// I keep running into error the sprite does not contain a definition for sprite. I still learning unity, so I am not sure I am missing code or not.