It looks like you're new here. If you want to get involved, click one of these buttons!
Hi, I'm a newbie for unity game develop. Recently I tried to make a card game. I made 11 cards with specify ID like Card1, Card2 to Card11 and are going to make more card.
Below is my part of the code in GameManager script
void StartGame()
{
player.Init(new List<int>() { 1, 3, 4, 4, 5, 1, 5, 5, 5, 6, 2, 3, 3, 3, 4, 6, 3, 8, 1, 1, 1, 9, 4, 9, 5, 3, 3, 1, 3, 2 });
enemy.Init(new List<int>() { 1, 4, 7, 6, 11, 2, 2, 7, 11, 4, 4, 3, 9, 1, 4, 4, 4, 1, 5, 6, 9, 3, 3, 3, 8, 4, 5, 8, 2, 4 });
}
and I make new script to get the random list in PrintRandomElements script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PrintRandomElements : MonoBehaviour
{
List<int> list = new List<int> { 1, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 7, 8, 9, 10, 11 };
List<T> GetRandomElements<T>(List<T> inputList, int count)
{
List<T> outputList = new List<T>();
for (int i = 0; i < count; i++)
{
int index = Random.Range(1, inputList.Count);
outputList.Add(inputList[index]);
}
return outputList;
}
void Update()
{
if(Input.GetMouseButtonDown(0))
{
var randomList = GetRandomElements(list, 30);
Debug.Log(string.Join(", ", randomList));
}
}
I can get the random number in list and I just need to copy and paste into GameManager script above. I'm searching a way to make it run random deck automatically. So that I can enjoy fighting with AI with random deck without need to copy and paste everytimes. I have searched many places but I can't find any good solution for this issue. Is it possible to do something like below:
void Start()
{
if(Input.GetMouseButtonDown(0))
{
var randomList = GetRandomElements(list, 30);
DeckA = Debug.Log(string.Join(", ", randomList));
DeckB = Debug.Log(string.Join(", ", randomList));
}
}
void Start()
{
player.Init(new List<int>() { DeckA });
enemy.Init(new List<int>() { DeckB });
}
Thank you.
Answers
Is it possible to mix 2 scripts into 1 script? So I will no longer need to copy and paste the long random list.
Hi, I'm not sure if I understand your problem quite well, but it seems to me that you can solve it like this:
Step 1 and 2 can also be done with a private variable, just use a GetComponent function
Wow! Thanks so much!! I have done the random deck.
https://youtu.be/n-Po2nCC4LA