Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 seanjust530_unity · Jul 07, 2018 at 01:01 AM · instantiategameobjectslistscard

How to Instantiate Objects based on a list of integers

Hey, I've made multiple games now including a platformer, pong/breakout style game, and a top-down game. However, I'm still learning C# and I'm stuck trying to make a DeckController script for my card game. Originally I had created an array for the deck but realized that an array would cause problems when I started trying to add and remove values from the array so now I'm using a list.

I have assigned integers to the list, each as a seperate card id and I was able to successfully shuffle the list . However I have no clue how to instantiate gameobjects to a position in the ui or scene for each card based on the integers I assigned. I am having loads of trouble and I know a card game is difficult to make but it's what I'm interested in.

Hopefully someone can help me or put me on the right path to learn since I'm not the best at working with the Unity classes and namespaces.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class DeckController : MonoBehaviour {
 
     int FireballID = 1;
     int PyroblastID = 2;
     int IceBlockID = 3;
 
     public List<int> Deck = new List<int>();
 
 
     public void Shuffle(List<int> DeckToShuffle)
     {
         for (int i = 0; i < DeckToShuffle.Count; i++)
         {
             int temp = DeckToShuffle[i];
             int randomIndex = Random.Range(i, DeckToShuffle.Count);
             DeckToShuffle[i] = DeckToShuffle[randomIndex];
             DeckToShuffle[randomIndex] = temp;
         }
     }
 
     // Use this for initialization
     void Start () {
         Deck.Add(PyroblastID);
         Deck.Add(FireballID);
         Deck.Add(IceBlockID);
         Shuffle(Deck);
     }
     
     // Update is called once per frame
     void Update () {
         
     }
 }
Comment
Add comment · Show 4
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 JVene · Jul 07, 2018 at 06:02 AM 1
Share

It's curious to me that they call it a list, because from a more general CompSci perspective a list usually implies a linked list, which this isn't (fortunately). It is a generic (that is type independent) implementation of an array, but you're right that it is better suited than a simple array because of the features of the container.

I would recommend you consider making a card class, and think of the deck as a list of cards, not a list of integers. Doing this means you can include information about each card's location (possibly a GameObject link, which then has it's own location information and perhaps artwork).

Further, if the deck is larger than 3 cards, there are many ways to shuffle - but the one you've posted might not work so well on a large deck. One fairly efficient means would be to include, in a card class, a numeric value (perhaps a 64 bit integer or float), I might call "order" or "deckposition". To shuffle, you could then rip through the list (from first to last element), assigning a random value to this deckposition member of each card. Then, sort the list by deckposition. Doing this sorts the deck, but by a random value, meaning shuffled. Repeating the process a random (few) times ensures a shuffled deck, and it's fairly quick, rather more easily written.

avatar image BastianUrbach JVene · Jul 07, 2018 at 07:45 AM 0
Share

I wouldn't say List implies a linked list. It just means that the length can change. List is an interface (not in C# but generally in Computer Science) and Array Lists are a common and fast implementation of it. Adding and removing elements at the end has an amortized cost of O(1) but you still get O(1) for random access.

avatar image JVene BastianUrbach · Jul 07, 2018 at 03:05 PM 0
Share

I hesitated to respond because I don't want my text to seem rude or confrontational, but if you'll accept this reply in the spirit of discussion I'll take the risk.

While I understand your point about C#'s List, the problem I have is that works dating from Don $$anonymous$$nuth's legendary encyclopedia on computer science from the late 60's through all texts that reference data structures and related algorithms into the 21st century, the word list is exclusively reserved for structures using links precisely to avoid confusion about the implications of the underlying structures on performance when compared to structures like arrays. $$anonymous$$y own college days were in the 80's, which prompts a perspective on the use of the word "List" as an implication that promotes confusion, but those learning computer science from C# (or Java) are induced into a counter vocabulary. Even in the context of C#, an array list represents a redundant phrase when one accepts that List is implemented as an array. It was also a point of contention, at one point from the 80's, to refer to binary trees as linked list structures, which they are, because the performance and method of implementing the tree is so different from that of a linked list. In a way this is like an English speaking student of French learning that zero is an egg ;) Borland was once criticized for creating an unordered container they called a bag, which had no relevance to implementation and performance characteristics as a result (from their OWL library in the early 90's).

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding prefabs to a list or an array from a folder and instantiating them. 2 Answers

adding components to a gameObject in a list of lists 2 Answers

I am trying to spawn coloured cubes that match a pixel image. At the moment the cubes spawn but i do not know how to match up the colours. 0 Answers

Instantiating 20K gameobjects performance issue 3 Answers

How do I add new instances of a object into a list? 1 Answer


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