Search Unity

Question Unity error CS0104 when entering Debug.Log("...")

Discussion in 'Scripting' started by zo8j2020, Jun 20, 2020.

  1. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    I was implementing a system into my rhythm game project where the GameManager will tell if a note has been hit or missed:
    Code (CSharp):
    1.  public void NoteHit()
    2.     {
    3.        Debug.Log("Hit On Time");
    4.      
    5.     }
    6.  
    7.     public void NoteMissed()
    8.     {
    9.         Debug.Log("Missed Note");
    10.  
    11.     }
    I have more code but these appear to be the lines that cause problems. After saving the script I went back to Unity and saw the error
    "
    Assets\Scripts\GameManager.cs(42,8): error CS0104: 'Debug' is an ambiguous reference between 'UnityEngine.Debug' and 'System.Diagnostics.Debug' "
    on both lines which feature the Debug.Log("...") code.

    I have tried changing them to UnityEngine.Debug.Log (or something similar to that) and although it stopped the errors, both the "Hit On Time" and "Missed Note" logs appeared on Console despite hitting buttons on time.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    I guess that you or some other code in your game must also have another class called Debug.

    If you intend to use
    UnityEngine.Debug( "hit...");
    , you just have to write it just like I did now.
     
    zo8j2020 likes this.
  3. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    I tried replacing the
    Debug.Log("hit...");
    with
    UnityEngine.Debug("hit...");
    , however I received this error instead:

    Assets\Scripts\GameManager.cs(42,20): error CS1955: Non-invocable member 'Debug' cannot be used like a method.
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You probably have this at the top of your class:
    Code (CSharp):
    1. using System.Diagnostics
    You probably added this by accident, and can most likely remove it safely.
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You're missing the .Log part :D
     
    Kurt-Dekker likes this.
  6. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Ah, thank you! Unfortunately, I'm still receiving both the "Hit On Time" and "Note Missed" messages on Console despite deliberately hitting notes correctly. :confused:

    Annotation 2020-06-20 010649.png
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    Totally my bad... thanks @PraetorBlue ... I left out the
    .Log
    part.

    As for the both messages, you should put more Debug.Log() statements in to find out why, or perhaps post the relevant code.
     
  8. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Well, I do have the code from another script (which handles the notes being hit) and where it tells GameManager when a note is hit/missed:
    Code (CSharp):
    1.   // Update is called once per frame
    2.     void Update()
    3.     {
    4.         if(Input.GetKeyDown(keyToPress))
    5.         {
    6.             if(canBePressed)
    7.             {
    8.                 gameObject.SetActive(false);
    9.  
    10.                 GameManager.instance.NoteHit();
    11.             }
    12.         }
    13.      
    14.     }
    15.  
    16.     private void OnTriggerEnter2D(Collider2D other)
    17.     {
    18.         if(other.tag == "Activator")
    19.         {
    20.             canBePressed = true;
    21.         }
    22.     }
    23.  
    24.  
    25.     private void OnTriggerExit2D(Collider2D other)
    26.     {
    27.         if (other.tag == "Activator")
    28.         {
    29.             canBePressed = false;
    30.  
    31.             GameManager.instance.NoteMissed();
    32.         }
    33.     }
    34.  
    35. }
    36.  
    37.  
    Hopefully this can help a little?
     
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,697
    If you hit a given note, you don't have a mechanism whereby the exit trigger would knows this and does not print the missed note when you exit the trigger.

    To fix: when you hit the note, set a boolean true (inside that particular note, obviously), then when you leave the trigger check that boolean before you blindly call missed.