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 Der_Kevin · Nov 14, 2016 at 03:50 PM · operatorpercentagepercent

keep 10% of an array?

hey! i have this huge list of arrays:

alt text

in my

  public Vector3[]    paths;  

but i wanna kick out some of them. so that i keep 10% of that. how can i do this? i thought i can use a % operator (https://msdn.microsoft.com/en-us/library/0w4e0fzs.aspx) like this:

  optimizedpoints = new Vector3[paths.Length%10];
  
          int j = 0;
  
          for (int i = 0; i < paths.Length; i%=10) {
              optimizedpoints[j] = paths[i];
              j++;
          }
 

but that seams to be wrong somehow? do someone know why?

vec3.jpg (157.7 kB)
Comment
Add comment · Show 2
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 hexagonius · Nov 14, 2016 at 04:14 PM 0
Share

i is not incremented at all and stays 0. the % works different than you expect. it gives you the rest of a division, so creating an array with length 100 % 10 gives you 0, because it works out evenly. you should initialize the array with $$anonymous$$atf.Round(Lenght * .9f) and run the for loop with I < that value and i++

avatar image Owen-Reynolds · Nov 14, 2016 at 04:23 PM 0
Share

The problem is you're trying to use modulo like it's magic, when it's really just a normal math operation.

Suppose you have 300 things in the array. 1/10th of that is 30. What math equation, that you already know, will turn 300 into 30? In the loop, if you start at 0 then want to go to 10, then 20 then 30; what math equation will turn 10 into 20, then turn 20 into 30, and so on?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by spraw · Nov 14, 2016 at 05:00 PM

I used your code as the base:

 void CreateNewArray()
     {
         int nStep = 10;
         optimizedArray = new Vector3[originalArray.Length / nStep + 1];
 
         int j = 0;
         for (int i = 0; i < originalArray.Length; i += nStep)
         {
             optimizedArray[j] = originalArray[i];
             j++;
         }
     }

I wouldn't do it like this, personally. I would just use Lists, so much more convenient. Like so:

 void WithoutLinq()
     {
         int nStep = 10;
         List<Vector3> optimizedList = new List<Vector3>();
         for (int i = 0; i < originalArray.Length; i += nStep)
         {
             optimizedList.Add(originalArray[i]);
         }
     }

Or

 void WithLinq()
     {
         //using System.Linq at the top of the class
         int nStep = 10;
         List<Vector3> optimizedList = originalArray.ToList().Where((x, i) => i % nStep == 0).ToList();
     }
Comment
Add comment · 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

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

60 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

Related Questions

Percentage sign 2 Answers

Is it possible to use . Operator to access variables in classes in lists 1 Answer

Why does unity subtract ammunition per frame? 1 Answer

And/Or Operator C# 2 Answers

how to make the score update showing the current points after they are subtracted for leaving the map or colliding with an object 0 Answers


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