Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Ochreous · Dec 23, 2014 at 06:27 PM · c#rotationgameobject

C# Rotate GameObjects Regardless of List Size

I have a script that rotates gameobjects in opposite directions. However I can only get a maximum of four gameobjects to rotate. So when for example I add six gameobjects to gameObjectList the last two gameobjects don't rotate. Is there a way I can get my script to rotate all of the gameobjects within gameObjectList regardless of list size?

             for (int i = 0; i < gameObjectList.Count -1; i+= gameObjectList.Count/2){
                 gameObjectList[i].Rotate( Vector3.forward, - fSpeed / fDistance);
                 gameObjectList[i+1].Rotate( Vector3.back, - fSpeed / fDistance);
             }
Comment
Add comment · Show 3
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 Adre76 · Dec 23, 2014 at 07:14 PM 0
Share
 for (int i = 0; i < gameObjectList.Count -1; i+= gameObjectList.Count/2)

Are you sure this is correct ?

avatar image zharik86 · Dec 23, 2014 at 07:17 PM 0
Share

Try this:

  for (int i = 0; i < gameObjectList.Count -1; i= i+2) {
   ...
avatar image DanSuperGP · Jan 08, 2015 at 12:39 AM 0
Share

If gameObjectList.Count is 1, then it will not rotate at all with that loop.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by SnotE101 · Dec 23, 2014 at 07:19 PM

Beans, I believe your problem lies in your for loop. i < gameObjectList.Count - 1' This will stop the loop at the second to last object in your list. If your list has six objects and you add 3 to each object in your code i+= gameObjectList.Count/2` then your final line gameObjectList[i+1].Rotate( Vector3.back, - fSpeed / fDistance); adds another int those three making it only rotate 4 objects. I hope this helps. I would suggest using a normal for loop. for(int i = 0; i < gameObjectList.Count; i++)

Good Luck!

Comment
Add comment · Show 4 · 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 Ochreous · Dec 23, 2014 at 08:22 PM 0
Share

Any suggestions for how I could reach the next gameObject in gameObjectList? i+1 was how I reached the next gameObject and had it rotate in the opposite direction of i. If I use a normal for loop i+1 doesn't exist.

avatar image Adre76 · Dec 23, 2014 at 10:42 PM 0
Share

See zharik86 comment to the original question

avatar image zharik86 · Dec 24, 2014 at 07:36 AM 0
Share

@Adre76. It is interesting, how are your comment differs from $$anonymous$$e, except time?

avatar image Adre76 · Jan 07, 2015 at 11:51 PM 0
Share

You're right it doesn't. Sorry didn't notice

avatar image
0

Answer by DanSuperGP · Jan 08, 2015 at 12:23 AM

Rather than playing silly games with your loop counters, why not just do a regular loop, and just flip the direction every other step.

 float direction = 1.0f;
 foreach(GameObject go in gameObjectList)
 {
 
     go.Rotate( Vector3.forward * direction, - fSpeed / fDistance);
      direction *= -1.0f;
 
 }
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

28 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

Related Questions

C# GameObject Reverses Z Rotation 0 Answers

C# Preserving GameObjects' Previous Meshes 1 Answer

C# Gameobject Rigidbody Mouse Collision 1 Answer

C# Child GameObject Disabled Script SetActive(false) 1 Answer

C# Plane Detecting a Gameobject 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