For loop and conditionals...
Hi everyone,
I am having a problem with my code.
What is supposed to do is go trough a list affecting the first part of a list is supposed to be changed into type A, and the second part is supposed to assign type B.
This is my code
void AssignLetter ()
{
for (int i = 0; i< 20 ; i++)
{
if (i < 10 )
{
currrentDeck[i].letter = PlayerCard.CardType.A;
}
if (i >= 10)
{
currrentDeck[i].letter = PlayerCard.CardType.B;
}
}
}
I tried to debug the code, and what it does is, that when it enters the second if, it changes ALL the objects from the list to B. Even the one with index[0]. My logic was that it should change only the item index[i].
$$anonymous$$aybe all 20 decks are actually the same deck? I mean, how are you creating decks and letters? Are they new instances or are they all referencing the same object?
currentDeck is a public list of PlayerCard (a custom class in which has a letter value in it) there is onñy one currentDeck which contains 20 PlayerCards added in gameplay
Yes, but what did you put in currentDeck? It looks like you put the same reference into each item in the list.
var card = new PlayerCard();
for (int i = 0; i < 20; ++i)
currentDeck.Add(card); // All of them reference the same card.
This is the code that puts de list together
public void CreateDeck()
{
for (int i = 0; i < card1Start ; i++)
{
currrentDeck.Add(templateCards[0]);
}
}
templateCards is an array of diferent scriptable Objects, all from the same class: PlayerCard.
When you add an object to a list doesn't it create a new instance of the Object? What should I've put?
No it doesn't create a new instance (for reference types) when you add an object to a list.
Value types (ints, floats, structs: Vector3 etc) will be copied by value (and thus a new instance is created) but reference types (classes: strings, ScriptableObject, etc) will only copy the reference to the object.
Answer by Statement · Nov 11, 2015 at 01:51 PM
public void CreateDeck()
{
// This will add card1Start number of copies of a
// reference to templateCards[0], so disregardless
// of which card in cuRRRentDecks we use, we will
// always use that *same* card.
//
// currrectDeck[3] will be the same as currrentDeck[5]
// for example.
for (int i = 0; i < card1Start; i++)
{
currrentDeck.Add(templateCards[0]);
}
}
So, currrentDeck (with 3 R, nice, spice it up a little :)) will always reference the same card. Accessing any card in the current deck of cards, will give you the same card. Instead, consider instantiating a copy of the template card and use that instead:
public void CreateDeck()
{
// This will add new instances of templateCards[0].
// Each card in the deck references a unique card.
//
// currrectDeck[3] will be different from currrentDeck[5]
// for example.
for (int i = 0; i < card1Start; i++)
{
currrentDeck.Add(Instantiate(templateCards[0]));
}
}
Thanks for taking the time to help me. This was very helpfull, as well as educational to me...
Your answer
![](https://koobas.hobune.stream/wayback/20220612044639im_/https://answers.unity.com/themes/thub/images/avi.jpg)