- Home /
Debug.Log gives back "object" instead of value
I am trying to get into unity and wanted to log my current ammo count but when I do so it gives back "object" instead of the value. What did I do wrong?
public class Gun : MonoBehaviour
{
[SerializeField]
public int _currentAmmo;
void Update()
{
Debug.Log(_currentAmmo.ToString());
}
}
Answer by metalted · Dec 10, 2021 at 11:32 AM
Im not sure at the moment why it would log "object", instead of just a string with the ammo count. It will most likely have something to do with how the ToString() function works. Besides that, you don't need to turn the value into a string to log it. You could just do: Debug.Log(_currentAmmo);
or something like Debug.Log("Current Ammo: " + _currentAmmo);
I tried it without the ToString() function and it gives the same result.
I tested your script as it is posted here, and it just logs the ammo value for me. What exactly is your console output, is there more information ?
May be you are looking at the wrong line?
The first line the debug writes is the actual string you are writing., The second line is UnityEngine.Debug:Log (object)
Your answer
Follow this Question
Related Questions
Can I change the destination of selecting console entries 0 Answers
How to register if my Raycast hits nothing(Solved) 2 Answers
How to read Debug.Log when using Android? 3 Answers
OnTriggerEnter() incorrectly firing? 1 Answer
Debug.Log Maximum Length? 2 Answers