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

Started by
3 comments, last by jbadams 10 years, 2 months ago

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.

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.

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.

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!

BSc Computer Games Programming (De Montfort University) Graduate

Worked on Angry Birds Go! at Exient Ltd

Co-founder of Stormburst Studios

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

- Jason Astle-Adams

This topic is closed to new replies.

Advertisement