Scoremodifier Clamp not working

I tested out what would happen if I would give a negative score and it ended up going below zero which made me a little bit confused.

For anyone else that got a little confused by this, you have to save the variable that gets returned by Clamp method.

public void ModifyScore(int scoreModifier)

{

    score += scoreModifier;

    score = Mathf.Clamp(score, 0, int.MaxValue);

    Debug.Log(score);

}
2 Likes

Thank you for sharing this :grinning_face_with_smiling_eyes:

The reason why it doesn’t work as shown in the lecture is because the “value” argument of the method Mathf.Clamp (to which we pass “score”) is by value and not by reference, otherwise, it would have been possible. And since there doesn’t seem to be an overload of this method accepting that argument by reference, the only way to get this to work is like jefre described (using the return value).

This is an error in the lecture and should be corrected. Perhaps one of the TAs (like @Nina) could make a note in the lecture resources or something. It didn’t get picked up because the scores never went below 0 or as high as int.MaxValue. The correct way is like @jefre wrote it.

As a teaching assistant, I provide support for the content of our courses only. I’m neither responsible for the content nor am I able to edit/modify the courses. I’m forwarding your suggestion to Gary. :slight_smile:

Privacy & Terms