Debug.Log and changing in Console

I wonder, Why Debug numer 1 show label in console every time when i’m using Mouse scrollWheel,
and number 2 not., though value of CurrentWeapon is changing. That make me crazy, when i use solution numer 2, and I see that my program doesn’t work well.

using UnityEngine;
using System.Collections;

public class SwitchWeapon : MonoBehaviour
{
    int currentWeapon = 0;
    int maxWeapon = 2;
    int i = 0;
   
    void Update()
    {
        if (Input.GetAxis("Mouse ScrollWheel") > 0)
        {
            if (currentWeapon == maxWeapon)
                currentWeapon = 0;
            else
                currentWeapon++;
            i++;
        }
        Debug.Log("loop " + i + " CurrentWeapon " + currentWeapon); // numer 1
        Debug.Log(" CurrentWeapon " + currentWeapon);               // numer 2
    }
}

The console has the collaps option to not show duplications of the same message. Your second message will become such a duplicate after a short amount of time, which is not the case for the first one.
It is a really practically useful annoying misleading function.

I agree, in case when condition if(false), we have duplicate, but otherwise I thought that both messages will change, becouse we are changing values( i, currentWeapon) in one block if. So that make no sense. I still ton’t understand :smile:

now my variable is public, so I can see change,

The message of the second Debug.Log can be

  • CurrentWeapon 0
  • CurrentWeapon 1
  • CurrentWeapon 2

If those messages were written once in the console, they won’t appear anymore if you have the Collapse option enabled. You find this option at in the console window at the top left, between Clear and Clear on Play. Disable the Collapse option to get the expected result.
This has nothing to do with your code!

1 Like