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 /
  • Help Room /
avatar image
0
Question by Merrick20 · Nov 10, 2015 at 10:00 PM · lists

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].

Comment
Add comment · Show 5
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 Jessespike · Nov 10, 2015 at 10:06 PM 1
Share

$$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?

avatar image Merrick20 · Nov 10, 2015 at 10:27 PM 0
Share

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

avatar image Statement Merrick20 · Nov 10, 2015 at 11:51 PM 0
Share

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.
avatar image Merrick20 · Nov 11, 2015 at 12:56 AM 0
Share

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?

avatar image Statement Merrick20 · Nov 11, 2015 at 01:55 PM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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]));
     }
 }
Comment
Add comment · Show 1 · Share
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 Merrick20 · Nov 11, 2015 at 02:28 PM 0
Share

Thanks for taking the time to help me. This was very helpfull, as well as educational to me...

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

35 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

Related Questions

Increase List of Spawns Dynamically 1 Answer

List.Add is not working 1 Answer

a list with a constructor class that also contains a list. 1 Answer

Can Static classes have static lists? 1 Answer

List give a count of zero when it have 1 element 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