• Advertisement
Sign in to follow this  

increment & loop function in this code (SOLVED, ignore topic)

This topic is 1450 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

function EnemySearch ()
{
for (i = 0; i < 5; i++) // what does this do? the rest of the code (seems to me like it) is based off the enemyDistance = Random.Range (1, 10)
      enemyDistance = Random.Range (1, 10);
      
      if (enemyDistance >= 8) // like this for example. makes use of the number provided from random.range. 

Share this post


Link to post
Share on other sites
Advertisement

Its doing whatever your code is meant to do, 5 times.  There's nothing more to answer.

 

I wont ask why you are making up random numbers, it doesnt look like your function is of much use to anything other than perhaps a partially finished unit test.

Edited by d4n1

Share this post


Link to post
Share on other sites

Its doing whatever your code is meant to do, 5 times.  There's nothing more to answer.

 

I wont ask why you are making up random numbers, it doesnt look like your function is of much use to anything other than perhaps a partially finished unit test.

just learning the basics of java scripting in unity via tutorials. Its not meant to be anything yet. Just trying to understand terms and ways to use them.

Share this post


Link to post
Share on other sites

As d4n1 says, the for loop will do whatever you specify it to 5 times.

 

In this case, you'll get 5 printed statements, such as:

 

The enemy is getting closer!

The enemy is getting closer!

The enemy has terrible breath!

The enemy is way over there!

The enemy has terrible breath!

 

 

If you were to remove the for loop, what you're doing is the same as calling that function (EnemySearch) 5x in your main function. Just makes your code considerably smaller you see ;)

 

As a test of your understanding, why not try doing the same with a 'while' loop? I'll let you figure out what condition you need in the while loop!

Share this post


Link to post
Share on other sites

As a side-note for future reference, Java and JavaScript are very different languages with no relation to each other, and confusing one with the other when asking for help is likely to result in confusing or incorrect answers.

 

Unless you're in the unusual position of using both languages on your current project, you don't want to tag your forum topics with the 'Java' tag when you're asking about 'JavaScript' (or vice versa).  (Note: I've fixed the tags on this topic so that it longer has this problem.)

 

smile.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement