Search Unity

Question Error with Debug.Log messages on Console!

Discussion in 'Scripting' started by zo8j2020, Jun 22, 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 and also missing them deliberately.
     
  2. Hiktses

    Hiktses

    Joined:
    May 30, 2020
    Posts:
    19
    Can you send the full code?
     
    zo8j2020 likes this.
  3. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Remove "using System.Diagnostics;" from the top of your file. You added it accidentally.

    As for the other issue, you have a bug in your code somewhere.
     
    nikkums, zo8j2020 and Hiktses like this.
  4. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    @PraetorBlue @Hiktses Alright, these are the codes associated with the logging of hits/misses.

    This one is from the GameManager which controls the scrolling of notes, music, etc.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class GameManager : MonoBehaviour
    6. {
    7.     public AudioSource theMusic;
    8.  
    9.     public bool startPlaying;
    10.  
    11.     public BeatScroller theBS;
    12.  
    13.     public static GameManager instance;
    14.  
    15.  
    16.     // Start is called before the first frame update
    17.     void Start()
    18.     {
    19.         instance = this;
    20.     }
    21.  
    22.     // Update is called once per frame
    23.     void Update()
    24.     {
    25.         if (!startPlaying)
    26.         {
    27.             if (Input.anyKeyDown)
    28.             {
    29.                 startPlaying = true;
    30.                 theBS.hasStarted = true;
    31.  
    32.                 theMusic.Play();
    33.             }
    34.  
    35.         }
    36.  
    37.  
    38.  
    39.  
    40.     }
    41.  
    42.     public void NoteHit()
    43.     {
    44.         UnityEngine.Debug.Log("Hit On Time");
    45.     }
    46.  
    47.     public void NoteMissed()
    48.     {
    49.         UnityEngine.Debug.Log("Missed Note");
    50.     }
    51.  
    52. }
    53.  
    54.  
    55.  
    56.  
    And this is the script which controls the individual notes:

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class NoteObject : MonoBehaviour
    7. {
    8.     public bool canBePressed;
    9.  
    10.     public KeyCode keyToPress;
    11.  
    12.     // Start is called before the first frame update
    13.     void Start()
    14.     {
    15.        
    16.     }
    17.  
    18.     // Update is called once per frame
    19.     void Update()
    20.     {
    21.         if(Input.GetKeyDown(keyToPress))
    22.         {
    23.             if(canBePressed)
    24.             {
    25.                 gameObject.SetActive(false);
    26.  
    27.                 GameManager.instance.NoteHit();
    28.             }  
    29.            
    30.         }
    31.        
    32.        
    33.     }
    34.  
    35.     private void OnTriggerEnter2D(Collider2D other)
    36.     {
    37.         if(other.tag == "Activator")
    38.         {
    39.             canBePressed = true;
    40.  
    41.             GameManager.instance.NoteHit();
    42.         }
    43.     }
    44.  
    45.  
    46.     private void OnTriggerExit2D(Collider2D other)
    47.     {
    48.         if(other.tag == "Activator")
    49.         {
    50.             canBePressed = false;
    51.            
    52.             GameManager.instance.NoteMissed();
    53.  
    54.         }
    55.     }  
    56. }
    57.  
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    I don't think you want the NoteHit() call on line 41. That registers every note as a hit, even if the user didn't press the button. You're already handling notes being hit on line 27.
     
    Joe-Censored and zo8j2020 like this.
  6. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Ah, thank you! I got rid of the NoteHit() call on line 41 and it fixed my constant NoteHit() issues. I still need to find a solution to the constant NoteMissed() call though!
     
  7. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Is it possible that deactivating the gameObject is triggering OnTriggerExit2D to be called? I actually don't know what happens when you deactivate a collider while it's inside of a trigger. That might be triggering your OnTriggerExit.
     
  8. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Hm, I think I can see whee you're coming from. Unfortunately I need that code so that the notes disappear when they're pressed within the "hit area" or whatever it's called, apologies for not knowing the correct term. :oops: Do you know of any way I could try and work around it?
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Maybe add an extra bool flag to the notes to keep track of if they were successfully hit? Then in OnTriggerExit, you can check that flag, and if true, you can avoid calling GameManager.instance.NoteMissed();
     
    zo8j2020 likes this.
  10. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Thank you! Out of interest, would it look like this?
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class NoteObject : MonoBehaviour
    7. {
    8.     public bool canBePressed;
    9.  
    10.     public bool NoteHit;
    11.  
    12.     public KeyCode keyToPress;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.    
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if(Input.GetKeyDown(keyToPress))
    24.         {
    25.             if(canBePressed)
    26.             {
    27.                 gameObject.SetActive(false);
    28.  
    29.                 GameManager.instance.NoteHit();
    30.        
    31.                 NoteHit = true;
    32.  
    33.             }
    34.        
    35.         }
    36.    
    37.    
    38.     }
    39.  
    40.     private void OnTriggerEnter2D(Collider2D other)
    41.     {
    42.         if (other.tag == "Activator")
    43.         {
    44.             canBePressed = true;
    45.  
    46.  
    47.         }
    48.    
    49.  
    50.     }
    51.  
    52.  
    53.     private void OnTriggerExit2D(Collider2D other)
    54.     {
    55.         if(other.tag == "Activator")
    56.         {
    57.             if(NoteHit = false)
    58.             {
    59.                 canBePressed = false;
    60.  
    61.                 GameManager.instance.NoteMissed();
    62.             }
    63.  
    64.         }
    65.     }
    66. }
    67.  
    Would it be better if I instead used " if(NoteHit = true) else " or would it do the same job?

    Edit: I tried running this code however it was still continuing to show NoteMissed() ^^;
     
    Last edited: Jun 23, 2020
  11. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    You're close. A couple issues with that code:

    On line 43 you're setting NoteHit = true. But that area is just when the note enters the "hit area", not when the player actually hit the note. You should really set that around line 28 instead. Remember the bug before with printing "Note Hit" when it wasn't?

    On line 56 you're using "=", but that's the assignment operator and is not appropriate here. If you want to check for equality you should use "==" like this:
    Code (CSharp):
    1. if (NoteHit == false)
    If you want to get really fancy, you could avoid the equality operator altogether. the "if" statement is looking for a true or false value, so you could just negate the value of NoteHit with the "!" operator and let "if" take care of the rest:
    Code (CSharp):
    1. if (!NoteHit)
    Last thing is that you should leave the "canBePressed = false" part on line 59 OUTSIDE of the if statement on line 57 (move it up above line 57). Otherwise you'll be able to continue hitting the note even if the player missed it.
     
    Last edited: Jun 23, 2020
  12. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    @PraetorBlue
    Ah, I see. I made changes to the code here:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class NoteObject : MonoBehaviour
    7. {
    8.     public bool canBePressed;
    9.  
    10.     public bool NoteHit;
    11.  
    12.     public KeyCode keyToPress;
    13.  
    14.     // Start is called before the first frame update
    15.     void Start()
    16.     {
    17.      
    18.     }
    19.  
    20.     // Update is called once per frame
    21.     void Update()
    22.     {
    23.         if(Input.GetKeyDown(keyToPress))
    24.         {
    25.             if(canBePressed)
    26.             {
    27.                 gameObject.SetActive(false);
    28.  
    29.                 GameManager.instance.NoteHit();
    30.  
    31.                 NoteHit = true;
    32.             }
    33.          
    34.         }
    35.      
    36.      
    37.     }
    38.  
    39.     private void OnTriggerEnter2D(Collider2D other)
    40.     {
    41.         if (other.tag == "Activator")
    42.         {
    43.             canBePressed = true;
    44.  
    45.  
    46.         }
    47.      
    48.  
    49.     }
    50.  
    51.  
    52.     private void OnTriggerExit2D(Collider2D other)
    53.     {
    54.         if(other.tag == "Activator")
    55.         {
    56.             if(!NoteHit == true)
    57.             {
    58.                 canBePressed = false;
    59.  
    60.                 GameManager.instance.NoteMissed();
    61.             }
    62.  
    63.         }
    64.     }
    65. }
    66.  
    Unfortunately, despite this the NoteMissed call continues to show alongside NoteHit.
     
    Last edited: Jun 23, 2020
  13. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Hey @PraetorBlue, any further info on what could be wrong?
     
  14. Try something like this (hope this works)

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. public class NoteObject : MonoBehaviour
    4. {
    5.     public bool canBePressed;
    6.     public bool noteHit;
    7.     public KeyCode keyToPress;
    8.  
    9.     void Start()
    10.     {
    11.         canBePressed = false;
    12.         noteHit = false;
    13.     }
    14.  
    15.     void Update()
    16.     {
    17.         if(canBePressed && Input.GetKeyDown(keyToPress))
    18.         {
    19.             gameObject.SetActive(false);
    20.             GameManager.instance.NoteHit();
    21.             noteHit = true;
    22.         }
    23.     }
    24.  
    25.     private void OnTriggerEnter2D(Collider2D other)
    26.     {
    27.         if (!other.CompareTag("Activator"))
    28.             return;
    29.         canBePressed = true;
    30.     }
    31.  
    32.     private void OnTriggerExit2D(Collider2D other)
    33.     {
    34.         if (!other.CompareTag("Activator"))
    35.             return;
    36.         if (!noteHit)
    37.             GameManager.instance.NoteMissed();
    38.         canBePressed = false;
    39.         noteHit = false;
    40.     }
    41. }
    Please note that my refactor function has changed some capitalization too, so be careful with the variable names.

    But I also have a question. This line:
    Code (CSharp):
    1. gameObject.SetActive(false);
    Why do you want to disable the current game object?
     
    Last edited by a moderator: Jun 24, 2020
  15. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    Thank you! I'll try it later on. Also, I want to disable the game object so that the note disappears when it is hit within the correct timing.
     
  16. zo8j2020

    zo8j2020

    Joined:
    Jun 18, 2020
    Posts:
    31
    @Lurking-Ninja Hello, I tried the code you recommended but nothing seems to have changed.