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
0
Question by RetepTrun · Sep 08, 2011 at 07:46 PM · arrayvariabletankelement

Bulletin Array, change element variables

I am making a game of tanks, which therefore involves a lot of wheels. Rather than make a new script for each vehicle I want to make just one flexible script.

I have gotten as far as making a bulletin array, but I don't know how to change the torque of wheels when they are inside an array.

 var wheelsleft:WheelCollider[];
 
 function Start(){
 }
 
 function Update () {}
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

1 Reply

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

Answer by Adam-Buckner · Sep 08, 2011 at 08:14 PM

To change the values within an array, you must "iterate" through it (or over it - both seem to be correct terms).

A common way to do this is with a "for" loop. You can also do a "foreach" loop.

     // This will print the Game Object name of everything in the array:
     var myArray : GameObject [];
     for (var i : int = 0; i < myArray.Length; i++) {
         Debug.Log (myArray[i].gameObject.name);
     }

     // You can include many operations within this for loop, including tests:
     for (var i : int = 0; i < myArray.Length; i++) {
         if (myArray[i] == someValue) {
             myArray[i] = someNewValue; 
         }
     }

     // This will go through all of the contents of an array and do something to it.
     // In this case it will turn all of the red lights off:
     var myLights : Light [];
     foreach (light : Light in myLights) {
         if (light.color == Color.red) {
             light.enabled = false;
         }
     }

In your case, you will need to run through all of your wheels with a for or foreach loop and do something to it...

The only note I would throw out there is: Do you need to work all the wheels? Can you "drive" your tanks in a different manner, and then simply run the wheels as a visual effect?

Comment
Add comment · Show 3 · 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 Adam-Buckner ♦♦ · Sep 08, 2011 at 08:23 PM 1
Share

A note on how these work:

The "foreach" is pretty obvious: "For each light in lights, if the light is red, turn it off".

The "for" loop is a little more arcane. There is the for loop "for (something)"... Something in this case it the incrementation of an int. This is saying: "Take a value, say "i", and set it to 0. If "i" is less than the length of my array, keep going - otherwise stop. When the loop is over, increment "i" by one. Now, in the block of code, the code is looking for a specific "slot" in the array when it looks at myArray[slot number]. Because "i" starts at 0, and then increments up by 1 each pass of the loop, by asking "Look at myArray[i];", you are saying "Look at myArray[0];" and then it loops, adds one to "i", and then you are asking "Look at myArray[1];". When "i" is greater than the length of the array, the loop stops. This way it looks at all of the slots in your array.

avatar image RetepTrun · Sep 09, 2011 at 12:53 AM 0
Share

Thanks for the reply, I didn't find the script refrence thorough enough. I'll be back to with the code when its going.

Yes I could power just 4 wheels and have some dummies, or forgo the wheels for some kind of transform or raycast. But making lots of wheels in a line will give more of a track effect and powering all of them will make sure theres traction wherever the tracks are touching.

avatar image Adam-Buckner ♦♦ · Sep 09, 2011 at 05:49 PM 0
Share

Don't forget that you can drive your vehicle with wheel colliders that are not parented to any visible world object... and then have ALL the wheels dummies. You might even be able to get rid of wheel colliders altogether, and drive the vehicle some other way, tho', admittedly, one set of wheel colliders might be useful, maybe just 2 in the middle of the tank? If it works, it doesn't need to mimic real life.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make an array of Interactive Clothes in one variable 0 Answers

Using variables inside a GameObject[] array 1 Answer

Command all array variable values 1 Answer

Access variables, objects in array in another script? 2 Answers

"Talent Tree" global yes/false var and int? 3 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