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 /
avatar image
1
Question by HTH_jjpprogrammer · Dec 11, 2010 at 10:26 PM · javascriptscriptingbasicsfor-loop

What does the word for mean in javascript?

I am just starting in JavaScript and Unity. I came acrost this code in the FPS_tutorial. It finds all the RididBodys near a collision and blasts them away from collision. Here is the code.

//Find all nearby colliders
var colliders : Collider[] = Physics.OverlapSphere( transform.position,
explosionRadius );
//Apply a force to all surrounding rigid bodies.
**for( var hit in colliders )
{
if( hit.rigidbody )
{
hit.rigidbody**.AddExplosionForce( explosionPower,
transform.position, explosionRadius );
}
}
//If we have a particle emitter attached, emit particles for .5 seconds
if( particleEmitter )
{
particleEmitter.emit = true;
yield WaitForSeconds( 0.5 );
particleEmitter.emit = false;
}

The part I do not understand has two stars around it(which I added). What is the "for()" doing? Is it like a "if()"? Why do I not see the "hit in colliders" variable anywhere but in the the "for()"? What is asked in the if statement "if(hit.rigidbody)"? Is "hit" a list of things that have collided with something? Is that why the code says "hit.rigidbody.AddExplosionFroce()" to add a explosion force to rigidbodys that have been hit? Please don't assume I know anything about Unity or JavaScript in your answers.

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
4

Answer by Statement · Dec 11, 2010 at 10:38 PM

Clean your code, so it is easier to follow.

for( var hit in colliders )
{
    if( hit.rigidbody )
    {
        hit.rigidbody.AddExplosionForce( explosionPower, transform.position,
                                         explosionRadius );
    }
}

What is the "for()" doing?

It loops through every collider in colliders. The variable hit is for every loop assigned to the next collider. It is the same as:

for( var i = 0; i < colliders.Length; ++i ) { var hit = colliders[i]; // This is your "var hit in colliders"

 if( hit.rigidbody )
 {
     hit.rigidbody.AddExplosionForce( explosionPower, transform.position,
                                      explosionRadius );
 }

}

Is it like a "if()"?

No, but it does have a scope (the { and }) so it looks very much like an if. The for will repeat the code inside the scope for every item in your collection.

What is asked in the if statement "if(hit.rigidbody)"

It checks if the collider has a rigid body. It is the same as calling "if(hit.rigidbody != null)".

Is "hit" a list of things that have collided with something?

No, hit is one of the objects that you have in your collider collection. Since it is in a loop, hit changes each time the loop is run so it will repeat for every object in your collider collection. Just think that "hit" is going to be "all" your colliders, one at a time in the loop.

Is that why the code says "hit.rigidbody.AddExplosionFroce()" to add a explosion force to rigidbodys that have been hit?

Yes, it applies an explosion force to every rigid body that was attached to a collider in the collision test.


I think you could do some simple tests to boost your confidence in what the code is doing. Try this:

function Start() { // Create a collection of strings var collection = new String[3];

 // Put some data in it
 collection[0] = "Hello";
 collection[1] = "For";
 collection[2] = "Loop";

 print ("Test 1");

 // Test the for with enumeration
 for (var each in collection)
 {
     print ("each = " + each);
 }

 print ("Test 2");

 // Test the for with explicit control,
 // backwards for fun!
 for (var i = collection.Length - 1; i &gt;= 0; --i)
 {
     var each = collection[i];
     print ("i = " + i);
     print ("each = " + each);
 }

}

The output for the first test:

Test 1
each = Hello
each = For
each = Loop

and the second test:

Test 2
i = 2
each = Loop
i = 1
each = For
i = 0
each = Hello
Comment
Add comment · Show 8 · 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 HTH_jjpprogrammer · Dec 13, 2010 at 10:46 PM 0
Share

Thank you for answering my question. I am sorry I can only understand simple code right now. That is why I am asking questions so I will soon understand. So are you saying that once a "for()" is called the code continues and everything in the"()" loops? Is the variable "hit in colliders" one variable or is it "hit" "in" "colliders"? What I mean is, is "in" something like "=". If not, that would that mean that "var hit in colliders" is set to nothing every loop? Is hit a variable we make in this code or does it have a preassigned value, like transform does.

avatar image HTH_jjpprogrammer · Dec 14, 2010 at 04:44 AM 0
Share

One more thing, What is the "[]" after the "Collider" type of variable "collider". I have never seen something like that. I searched it the Unity scripting help but did not find anything. What is it?

avatar image Amir Ebrahimi · Dec 14, 2010 at 05:00 PM 3
Share

Both are good questions. Let's start with the "Collider[]" part first. The "[]" in that means that the variable "colliders" is an array. An array just means it holds many of one thing as opposed to one of one type of thing. Now, on to the "for()" part. Since you have many of one thing, you need to look at each one and consider it. This is why you have "for(var each in colliders)". This is saying, "for each of collider that I have in the colliders array, run this code between the brackets". Does this make a bit more sense now? You might also want to read up on: http://en.wikipedia.org/wiki/

avatar image HTH_jjpprogrammer · Dec 15, 2010 at 01:00 AM 0
Share

O$$anonymous$$, thank you, I understand what a "for()" is now. But what is a "hit"?

avatar image Statement · Dec 15, 2010 at 01:24 AM 0
Share

In your example, hit is a collider. It is just a variable taken from the colliders array. I think it is improperly named in this case and I agree "hit" raises some questions if you're new to for loops. If you rename "hit" to "collider", does it make more sense to you? for (var collider in colliders) { if (collider.rigidbody) .....

Show more comments

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

No one has followed this question yet.

Related Questions

(20,57): BCE0022: Cannot convert 'EnemyAI[]' to 'EnemyAI'. 1 Answer

Refining a 3d side scroller camera script 0 Answers

Passing sender object as an argument in AddListener method 1 Answer

for loop not working if I start at the end number. 1 Answer

Why can't i set my integer to -1? 2 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