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 /
avatar image
-1
Question by Dytdyt · Mar 13, 2017 at 06:55 AM · c#arrayarraysforeach

Array problems

Hi

I'm having some trouble understanding the use of arrays(or lists?). For an example i have 2 different arrays with Transforms called slaves[] and masters[]. I want to get the angle between the slave and master and afterwards i want to add torque to a rigidbody, i could do it manually like this: AngleError[1] = Vector3.Angle(slave[1].up,master[1].up); slaveRigidbody[1].AddTorque(slave[1].right*AngleError[1]); and go on: AngleError[2] = Vector3.Angle(slave[2].up,master[2].up); slaveRigidbody[2].AddTorque(slave[2].right*AngleError[2]); and so on...

But with 12 different masters and slaves it's pretty time consuming. Is there a way to make this happen with only a single piece of code instead of 12?

Thanks in advance :)

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
2

Answer by Suley · Mar 13, 2017 at 08:53 AM

Hi,

as far as i understand u want to do this in a loop, so u dont have to type the 12 iterations. So it will be something like this.

   for (int i = 0; i <= AngleError.Length; i++) 
     {
             AngleError[i] = Vector3.Angle(slave[i].up,master[i].up);        
             slaveRigidbody[i].AddTorque(slave[i].right*AngleError[i]);
     }


Note if u use List u need AngleError.Count;

If i understood ur Question wrong, sorry for the misunderstanding

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

Answer by Namey5 · Mar 13, 2017 at 08:51 AM

Rather than using a ForEach loop, simply use a For loop. A For loop is a loop which simply runs the same piece of code over and over again, passing that code a variable. The loop continues, increasing the variable each time until it reaches a set maximum. You can use for loops for different things, but that's the main way you would use it. So, in this case it would be;

 for (int i = 0; i < slave.Length; i++)
 {
     AngleError[i] = Vector3.Angle(slave[i].up,master[i].up);
     slaveRigidbody[i].AddTorque(slave[i].right*AngleError[i]);
 }

And btw, arrays start at 0, not 1.

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

Answer by BartHeq · Mar 13, 2017 at 08:53 AM

@Dytdyt why don't you use loop?

 for (int i = 0; i < slave.length; i++)
 {
     AngleError[i] = Vector3.Angle(slave[i].up, master[i].up);
     slaveRigidbody[i].AddTorque(slave[i].right * AngleError[i]);
 }

You also can use "while" loop if you want.

I hope it helps.

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Perform Action Foreach Variable of a Specific INT in an Array C# 2 Answers

Array foreach problem 1 Answer

I have to set a Collider[] to Collider - what do? 1 Answer

Conceptual Question on Creating Arrays at Runtime 1 Answer

Array index is out of range 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