Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Mar 03, 2021 at 12:38 AM by Mrroundtree22 for the following reason:

I figured it out.

avatar image
0
Question by Mrroundtree22 · Mar 02, 2021 at 08:49 PM · listloopvalue

Interchangeable value loop

 for (int i = 0; i < myList.Count; i++)
         {
             if (myList[i] == null)
             {
                 myList[i] = item;
                 break;
             }
         }

I want "i" to be an interchangeable number i.e. if I have 5 items I want 5 items to be added to myList. If have 7 it adds 7 etc.

Comment
Add comment · Show 6
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 pauldarius98 · Mar 02, 2021 at 08:56 PM 0
Share

Could you elaborate a bit more?

avatar image Mrroundtree22 pauldarius98 · Mar 02, 2021 at 09:04 PM 0
Share

Sure thing. What do you want me to elaborate on?

avatar image pauldarius98 Mrroundtree22 · Mar 02, 2021 at 09:08 PM 0
Share

Right now "i" is used to iterate trough the list, i don't get how would it be interchangeable. What should dictate how many elements to add to the list?

Show more comments

1 Reply

  • Sort: 
avatar image
0

Answer by pauldarius98 · Mar 02, 2021 at 09:34 PM

Ok, after the informations that you provides me i think i figured out what you need. So, in order to add an item to your list you can use myList.Add(item) If you want to add an item n times you can write

 for (int i= 0; i < n; i++)
 {
     myList.Add(item);
 }

Where n is the number of times to add the item

Comment
Add comment · Show 6 · 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 Mrroundtree22 · Mar 02, 2021 at 09:41 PM 0
Share

No, that's not it. This is how I mean:

 int value = 1;
 
 for (int i= 0; i < 10; i++)
  {
      myList.Add(item);
  }

Value is interchangeable, meaning, it isn't always going to be 1. But I want value to dictate how many item(s) get added. So if value = 4. The myList.Add(item) * 4.

avatar image pauldarius98 Mrroundtree22 · Mar 02, 2021 at 09:47 PM 0
Share

Well, you can set n to how many items you want to be added, just declare it like int n = 4; place whatever value that you need instead of 4. You can also change the value at runtime from code

avatar image Mrroundtree22 pauldarius98 · Mar 02, 2021 at 09:51 PM 0
Share

No, I just keep getting "ArgumentOutOfRangeException: Argument is out of range."

Show more comments
avatar image Bunny83 Mrroundtree22 · Mar 02, 2021 at 09:55 PM 1
Share

Your "value" is the variable "n" in paul's answer and his code does exactly that. If n == 4 the boop body will run 4 times. If you have trouble understanding how a for loop works I highly recommend you watch some general basic coding tutorials. Those are the absolute fundametal basics.


A for loop header has exactly 3 parts:

  • the initialization part

  • the for loop condition

  • the loop step postprocessing part.

A loop like this:

 for(int i = 0; i < n; i++)
 {
     // code
 }

Will roughly translate to a while loop that looks like this:

 {
     int i = 0;
     while (i < n)
     {
         // code
         i++;
     }
 }


avatar image Mrroundtree22 Bunny83 · Mar 02, 2021 at 10:01 PM 0
Share

I understand how loops work. It's just not the result I'm looking for. I'm explaining my problem as best I can but I don't know how to make it any clearer. I don't want "n" to change. "n" should stay the same size. I tried making it a nested loop like this:

  for (int x = 0; x < value; x++)
         {
             for (int i = 0; i < n; i++)
             {
                 if (myList[i] == null)
                 {
                     myList.Add(item);
                     break;
                 }
             }
         }

But this doesn't work either even though I believe it should.

Follow this Question

Answers Answers and Comments

116 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

A node in a childnode? 1 Answer

Loop through a GameObject list based on the size of the list 1 Answer

Vector3 List Find Contains Specific X Coordinate Value and Remove 0 Answers

Default value on List vanished 3 Answers

How to have a loop run itself again if returned value is a known exception, with a new return value. 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