Passing different arguments in a method.

So, i’m stuck. Again.

Okay, I have a variable that I want to pass through a method. However, this variable is meant for more than one object, so i’m using a foreach loop. Now, I want this variable to have a random value between 2 numbers, and it has to be random for each object. The value is random, yes, but when that one number is decided by the script, ALL the objects get the same value, since the code inside the foreach loop asks only for the value of “rdmValue”.(Check below) I’d like my script to make new values for each object.

  • This is the value I want to pass:
    rdmValue = Random.Range(0, 11);

  • Let’s say “rdmValue” equals 4. Now all the objects will get a 4, even though I want all of them to get a random value.
    foreach (GameObject object in enemyList)
    {
    object.GetComponent().ReceiveDamage(rdmValue);
    }

Please, let me know if you didn’t get something from what I typed(my English is not too great). Thank you!

Put “rdmValue = Random.Range(0, 11);” inside the loop.

–Eric

1 Like

Jesus, that actually was the answer haha. I feel dumb now. Anyway, thank you so much for the fast response!!

By the way, congrats on being the latest in a long line of people who write some variation of “my English is not very good” and then proceed to write better English than most native speakers can manage. :wink:

–Eric

3 Likes

Haha! Really? Well, thank you! :slight_smile: