- Home /
Populate game objects from a list?? (C#)
So I'm fairly new to unity and programming but my goal is to take the variables from my list items here:
List<Cards> cardValue = new List<Cards> ();
cardValue.Add (new Cards ("One", 1, 1, 1, 1));
cardValue.Add (new Cards ("Two", 2,2,2,2));
cardValue.Add (new Cards ("Three", 3,3,3,3));
cardValue.Add (new Cards ("Four", 4,4,4,4));
cardValue.Add (new Cards ("Five", 5,5,5,5));
cardValue.Add (new Cards ("Six", 6,6,6,6));
cardValue.Add (new Cards ("Seven", 7,7,7,7));
cardValue.Add (new Cards ("Eight", 8,8,8,8));
cardValue.Add (new Cards ("Nine", 9,9,9,9));
cardValue.Add (new Cards ("Ten", 10, 10, 10, 10));
And have these values pass onto "cards" in my scene but I am unsure how to go about doing this. In my scene I simply have 10 game objects all listed as Card1, Card2, etc up to Card10. I want to be able to pass "one" onto Card1 along with the values in that line, "Two" onto Card2 and it's values, Etc.
Any solution or even a nudge in the right direction would be so helpful :(
Answer by taylank · Mar 24, 2014 at 01:15 AM
You probably want to have a single "Card" prefab, which you can instantiate as needed from another script that's on a GameManager sort of gameObject. On this Card prefab, you can have a behaviour that's called something like CardScript.cs and inside that, you can have public variables for whatever data you want your cards to have: i.e. string cardName; int cardValue; etc.
So you'd do something like:
CardScript cs = Instantiate("Card", position, rotation) as CardScript;
//and access its variables via cs
cs.cardName = cardValue[0].name;
Your answer
Follow this Question
Related Questions
melee system script problem, what did i do wrong or what to do next? 0 Answers
C# Rotate GameObjects Regardless of List Size 2 Answers
expecting (, found 'OnConnectedToServer'. 0 Answers
Explosion script question 0 Answers
C# Change Script Help 1 Answer